CPT loop returns empty when category ID is argumented

Hi. I don’t know wether or not this is a Sage issue or a WP issue, so I’ll just take the chance and post this here. I’m not an elite developer, so scanning through the Sage source files to solve this is, frankly, way over my head. It’s worth mentioning I have search “all over the net” regarding this for the last couple of hours and I’m getting mad now, as I can’t figure this out :sob: I wish to ask here, since I don’t want to break anything regarding Sage smart features.

I have registered a CPT named “store” and I try to filter out this data with a store category as argument. However, this don’t seem to work, neither with get_posts or WP_Query. Anyone got an idea way? The array returns empty.

This code returns it all:

<?php 
$args = array ( 'post_type' => 'store', 'posts_per_page' => 5);
$myposts = get_posts( $args );
foreach( $myposts as $post ) :    setup_postdata($post);
?>
<?php get_template_part('templates/content', 'stores'); ?>
<?php endforeach; ?>

This returns nothing:

<?php $args = array ( 'post_type' => 'store', 'category' => 9, 'posts_per_page' => 5); $myposts = get_posts( $args ); foreach( $myposts as $post ) : setup_postdata($post); ?> <?php get_template_part('templates/content', 'stores'); ?> <?php endforeach; ?>

Same as with WP_Query. Working:

<?php $args = array ( 'post_type' => array( 'store' ), ); $query = new WP_Query( $args );

if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();

echo the_permalink();
echo the_title();
}

} else {
}
wp_reset_postdata();
?>

Not working:

<?php $args = array ( 'post_type' => array( 'store' ), 'cat' => '9', ); $query = new WP_Query( $args );

if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();

echo the_permalink();
echo the_title();
}

} else {
}
wp_reset_postdata();
?>

Why, oh, why?!

Hey Raffen,

Try removing the quotes from the value in your category parameter. (also, no need to pass an array to post_type) ie:

$args = array (
   'post_type' =>  'store' ,
   'cat' => 9,
);
$query = new WP_Query( $args );

cat accepts an integer. See: http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters

Also, this is a pretty helpful reference to bookmark - http://www.billerickson.net/code/wp_query-arguments/

Hope this helps!

Hi, smutek. Thanks for helping out. I have tried it all, also without quotes. It just doesn’t work.

Hi ruffian you could try this out, also it’s worth noting that the “post_type” arg is not the category name but rather the name you pass when you register_post_type().

<?php //Display 5 Custom post types
$cpt_args = [
	'post_type' => 'cpt_name',
	'posts_per_page' => 5,
	
];

$cpt = new WP_Query( $cpt_args );
?>
<h3>Display Custom post type</h3>


	<?php while ( $cpt->have_posts() ) : $cpt->the_post(); ?>
		<h4 class="entry-title"> <a href="<?php the_permalink(); ?>"><?php the_title();?> </a></h4>
		<div class="entry-content">
			<?php the_excerpt();?>
		</div>
	<?php endwhile;
wp_reset_postdata();
	?>

@erikkowalski ! Hi. Thanks, but, maybe I was unclear. While using 'post_type' => 'store' only, it prints out everything I need. But if I do 'post_type' => 'store','cat' => 9,, nothing shows.

How did you add support for categories to the custom post type?

Does your cpt declare support for:

'taxonomies'         => array( 'category', 'post_tag' )

?

Hi :slight_smile: Here’s the CPT boilerplate I’m playing around with: http://pastebin.com/bYDNXyVL

Raffen, somewhat related aside - I use this for creating CPT’s -

It’s decent, quick, works well, and keeps the CPT’s out of the theme and in plugin land.

Your custom post type is using a custom taxonomy, imaginatively called store_category.

Follow the Codex on Taxonomy Parameters and you should be fine.