网站优化

网站优化

Products

当前位置:首页 > 网站优化 >

WordPress,查询未找到相关帖子

GG网络技术分享 2025-03-18 16:12 23


问题描述:

I have three posts, two which have the tag eGuide one which has the tag Article. When I click on an eGuide, I want the sidebar to display other eGuide\'s (up to 2) and nothing else. To do this, I have the following query:

global $post;

$args = array(

\'post_type\' => \'resources\',

\'category__in\' => wp_get_post_categories($post->ID ),

\'posts_per_page\' => 3,

\'post__not_in\' => array($post->ID )

);

$relatedPosts = new WP_Query( $args );

But it\'s showing the article too? I\'ve also tried:

\'post__not_in\'   => array(get_the_ID() )

... still no luck.

网友观点:

global $post;

$term_list = wp_get_post_terms( $post->ID, \'your_taxonomy_here\', array( \\\"fields\\\" => \\\"ids\\\", \\\"parent\\\" => 0 ) );

$args = array (

\'post_type\' => \'resources\',

\'posts_per_page\' => 3,

\'post__not_in\' => array( $post->ID ),

\'post_status\' => \'publish\',

\'tax_query\' => array(

array (

\'taxonomy\' => \'your_taxonomy_here\',

\'field\' => \'term_id\',

\'terms\' => $term_list[0],

),

),

);

$relatedPosts = new WP_Query( $args );

You can try this code to get related posts.

标签:

提交需求或反馈

Demand feedback