简体   繁体   中英

WP_Query get post by category id returns all categorries

I am trying to write a WP_Query that retrieves all posts in a certain category and display the posts on a blog page.

<?php
    $args  = array(
        'post_status' => 'publish',
        'cat' => 24,
        'order' => 'DESC'
    );
    $query = new WP_Query($args);
    while ( $query->have_posts() ):
        $query->the_post();
        get_template_part( 'templates/content/content', 'loop' );
    endwhile;
?>

Although the query returns only published posts in descending order, the category parameter is ignored (it returns the content loop template for all categories instead of just category 24).

This is the category URL from the admin panel: http://www.apokalipsa.si/wp-admin/term.php?taxonomy=category&tag_ID=24&post_type=post&wp_http_referer=%2Fwp-admin%2Fedit-tags.php%3Ftaxonomy%3Dcategory

I tried swapping the 'cat' => 24, query param with 'tag_ID' => 24, (from the category URL in the categories page) but the result is the same.

    <?php
    $args  = array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'category__in' => 24,
        'order' => 'DESC'
    );
    $query = new WP_Query($args);


    while ($query->have_posts()):
        $query->the_post();

        get_template_part('templates/content/content', 'loop');
    endwhile;

you can try this

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