• Capobis

    (@ioweuiraosjfosj)


    Hi,
    So this is pretty simple. I’m trying to display a specific post but only if it’s in the category. I was wondering why this code here doesn’t work (Meaning it will display the post even if it’s not in the category):

    query_posts( array ( 'category_name' => 'category', 'name' => 'hello-world' ) );
    while (have_posts()):
        the_post();
        get_template_part( 'content', get_post_format() );
    endwhile;

    So I hacked it to work like this. This will only display the hello-world post when it’s in the category.

    query_posts( array ( 'name' => 'hello-world' ) );
    while (have_posts()):
        the_post();
        if( in_category('category') ) get_template_part( 'content', get_post_format() );
    endwhile;

    I’m still wondering why the first code wouldn’t work though.

    Thanks!

  • The topic ‘query_posts not working with post name and category’ is closed to new replies.