I’m building a guitar lessons site and I have a question about Custom Post Types. I am creating a “Lessons” page which will be my main blog / loop. I also wanted to create a custom post type for “Arrangements” which will be just video / songs I arrange and play.
I’m using CPT UI for the custom post type called “Arrangements” and I’m trying to figure out how to create separate loops – Lessons = main post type, Arrangements = custom post type.
I have a arrangements.php template that has a loop like so:
$args = array(
'post_type' => array('post', 'arrangements'),
'post_status' => 'publish',
);
$new_post_loop = new WP_Query($args);
if ($new_post_loop->have_posts()) :
while ($new_post_loop->have_posts()) :
$new_post_loop->the_post();
get_template_part('template-parts/content', 'blog');
endwhile;
the_posts_navigation();
else :
get_template_part('template-parts/content', 'none');
endif;
?>
However, it’s pulling in all of my Lessons as well as the custom post type. Any idea of what I’m doing wrong? Thank you.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You don’t specify this, but if your lessons are being pulled in along with your arrangements then you must be using default WordPress posts for the Lessons… assuming that, here’s what’s happening in your code.
On the second line when you’re setting your query arguments ($args) you specify that you want BOTH the posts post type and your custom arrangements post type. So you get both.
Simple fix though, just use this for your $args.
$args = array(
'post_type' => 'arrangements,
'post_status' => 'publish',
);
You could still leave it as an array though, no harm…
$args = array(
'post_type' => array('arrangements'),
'post_status' => 'publish',
);
Give that a try and see if you get the desired result.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0