Php 通过WP_查询中的自定义分类术语值获取Woocommerce产品
我正在开发一个插件,它将放在我的woocommerce产品侧栏中。 我需要的是,给定产品id/对象,它将找到2个具有我以前创建的相同自定义分类法的产品 通过这段代码,我得到了产品中使用的术语列表,其中“collane”是自定义分类法Php 通过WP_查询中的自定义分类术语值获取Woocommerce产品,php,wordpress,woocommerce,product,custom-taxonomy,Php,Wordpress,Woocommerce,Product,Custom Taxonomy,我正在开发一个插件,它将放在我的woocommerce产品侧栏中。 我需要的是,给定产品id/对象,它将找到2个具有我以前创建的相同自定义分类法的产品 通过这段代码,我得到了产品中使用的术语列表,其中“collane”是自定义分类法 get_the_term_list( $product->id, 'collane', '<div style="direction:rtl;">', '</div>', '' ); 如何更改代码以获得所需的分类id/对象,然后在W
get_the_term_list( $product->id, 'collane', '<div style="direction:rtl;">', '</div>', '' );
如何更改代码以获得所需的分类id/对象,然后在WP_查询中使用它 您应该尝试以下
WP\u查询
$args
,该查询将允许从相同的“collane”自定义分类中获取两个与当前产品具有相同术语的其他产品
我正在使用WordPress函数从特定的自定义分类中获取帖子(这里是“产品”自定义帖子类型)中的术语ID
守则:
$taxonomy = 'collane'; // The targeted custom taxonomy
// Get the terms IDs for the current product related to 'collane' custom taxonomy
$term_ids = wp_get_post_terms( get_the_id(), $taxonomy, array('fields' => 'ids') ); // array
$query = new WP_Query( $args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => 2, // Limit: two products
'post__not_in' => array( get_the_id() ), // Excluding current product
'tax_query' => array( array(
'taxonomy' => $taxonomy,
'field' => 'term_id', // can be 'term_id', 'slug' or 'name'
'terms' => $term_ids,
), ),
);
// Test count post output
echo '<p>Posts count: ' . $query->post_count . '</p>';
// The WP_Query loop
if ( $query->have_posts() ):
while( $query->have_posts() ):
$query->the_post();
// Test output
echo '<p>' . $query->post->post_title . ' (' . $query->post->ID . ')</p>';
endwhile;
wp_reset_postdata();
endif;
$taxonomy='collane';//目标自定义分类法
//获取与“collane”自定义分类相关的当前产品的术语ID
$term_id=wp_get_post_terms(获取_id(),$taxonomy,数组('fields'=>'id'));//排列
$query=newwp\u查询($args=array)(
“post_类型”=>“产品”,
“发布状态”=>“发布”,
“忽略粘性帖子”=>1,
'每页发布'=>2,//限制:两种产品
'post\u not\u in'=>数组(获取\u id()),//不包括当前产品
“tax_query”=>数组(数组(
“分类法”=>$taxonomy,
'field'=>'term\u id',//可以是'term\u id','slug'或'name'
“术语”=>$term\u id,
), ),
);
//测试计数后输出
回显“Posts count:”.$query->post_count.”;
//WP_查询循环
如果($query->have_posts()):
而($query->have_posts()):
$query->the_post();
//测试输出
回显“”.$query->post->post_title.”(“.$query->post->ID.”)”;
结束时;
wp_reset_postdata();
endif;
谢谢,我正在测试它。现在我可以看出,在您的代码中,缺少一个括号,代码无法运行。
$taxonomy = 'collane'; // The targeted custom taxonomy
// Get the terms IDs for the current product related to 'collane' custom taxonomy
$term_ids = wp_get_post_terms( get_the_id(), $taxonomy, array('fields' => 'ids') ); // array
$query = new WP_Query( $args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => 2, // Limit: two products
'post__not_in' => array( get_the_id() ), // Excluding current product
'tax_query' => array( array(
'taxonomy' => $taxonomy,
'field' => 'term_id', // can be 'term_id', 'slug' or 'name'
'terms' => $term_ids,
), ),
);
// Test count post output
echo '<p>Posts count: ' . $query->post_count . '</p>';
// The WP_Query loop
if ( $query->have_posts() ):
while( $query->have_posts() ):
$query->the_post();
// Test output
echo '<p>' . $query->post->post_title . ' (' . $query->post->ID . ')</p>';
endwhile;
wp_reset_postdata();
endif;