最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to create a separate Loop for custom post types?

programmeradmin0浏览0评论

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.

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.

Share Improve this question asked Jan 8, 2021 at 13:25 sackadelicsackadelic 376 bronze badges 2
  • Are you using the default WordPress posts as "Lessons"? – Tony Djukic Commented Jan 8, 2021 at 15:33
  • Yes - sorry for any confusion. – sackadelic Commented Jan 8, 2021 at 19:02
Add a comment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论