Joseph Lariosa
Joseph Lariosa

Reputation: 365

How to display posts from specific custom post type category

I'm trying to display a post from a category I made inside a custom post type.

Here's my current loop code,

I want to display posts from awards category in announcements post type

<?php $loop = new WP_Query( array( 'posts_per_page' => 99,'post_type' => 'annoucements','orderby' => 'date','order' => 'ASC','ignore_sticky_posts' => 1, 'paged' => $paged ) ); if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php the_title()?>                                                         
<?php endwhile; endif; wp_reset_postdata();?>   

Upvotes: 3

Views: 10984

Answers (2)

Tanmay Patel
Tanmay Patel

Reputation: 1810

You can use this code for display posts from specific custom post type category(taxonomy).

According to extensive and long running trac ticket #12702, custom post types don't (and likely won't) support sticky functionality.

Please let me know if you found any issues in this code. Thanks.

<?php
    $options = array(
        'post_type' => 'annoucements',
        'posts_per_page' => 99,
        'orderby' => 'date',
        'order' => 'ASC',
        'paged' => $paged,
        'tax_query' => array(
            array(
                'taxonomy' => 'taxonomy_cat', // Here I have set dummy taxonomy name like "taxonomy_cat" but you must be set current taxonomy name of annoucements post type. 
                'field' => 'name',
                'terms' => 'awards'
            )
        )
    );
    $query = new WP_Query( $options );
    if ( $query->have_posts() ) :
        while($query->have_posts()) : $query->the_post();
            the_title();
        endwhile; wp_reset_postdata();
    endif;
    ?>

Upvotes: 5

Raunak Gupta
Raunak Gupta

Reputation: 10809

If you want to get the post from a category then you have to pass the category slug in WP_Query arguments.

$args = array(
    'posts_per_page' => 99,
    'post_type' => 'annoucements',
    'category_name' => 'awards', //<-- add this
    'orderby' => 'date',
    'order' => 'ASC',
    'ignore_sticky_posts' => 1,
    'paged' => $paged);
$loop = new WP_Query($args);
if ($loop->have_posts()) :
    while ($loop->have_posts()) : $loop->the_post();
        the_title();
    endwhile;
endif;
wp_reset_postdata();

Reference : WP_Query: Category Parameters

Hope this helps!

Upvotes: 2

Related Questions