Archived topic
I can't seem to get WP_QUERY to work on a custom post type content file
2 replies · Started by Josh on December 11, 2022
Hi,
I've just started working with WP_QUERY today and am trying to test a loop in a custom post type file called content-brand.php, which is the content display file for the "brand" CPT. I created this file by copying and renaming content-single.php.
Unfortunately, the code is not returning any posts at all.
Does GeneratePress prevent WP_QUERY from running in the PHP files for the theme?
Should I be doing this some other way with GeneratePress?
I have not found anything that says WP_QUERY can only be run on a certain file type or on a certain part of a PHP file and I can get other WP functions like get_the_title() to work fine, so I'm kind of confused.
This is the code I am placing into the file:
<?php
// args
$args = array(
'post_type' => 'brands',
'posts_per_page' => 3
);
// query
$the_query = new WP_Query($args);
?>
<?php if( $the_query->have_posts() ): ?>
<ul>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
Thank you in advance for any assistance with this. I am trying to land a gig that requires use of the WP_QUERY inside GeneratePress so I would be grateful for any help.
Josh
OK, disregard. I used a shortcode inside a content template element. I had to delete the PHP files associated with the Brand custom post type for the element to work.
I assume that is normal?
Thank you for a great product!
Glad to hear that you've found a solution.