简体   繁体   中英

WordPress, query not finding related posts

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM