Search code examples
wordpresslistpostpagination

Wordpress get custom post type list without parent


I've got a Wordpress with custom post type recipe, I order the post by making a parent post for kind of recipe (like fish) and then I make the post with the recipe.

I'd like to make a list of all the post but without the parent pages. There not that much problem with that, but I need to get a paging that will work with that. Is it possible?

I tried listing all the post and just skip the parent page but the pagination doesn't work with it.

Here is the code

<?php 
    $args = array( 'post_type' => 'recipe', 'posts_per_page' => 3, 'paged' => $paged );
    $loop = new WP_Query( $args );
    while ($loop->have_posts()) : $loop->the_post(); 
       $args = array(
               'post_type' => 'recipe',
               'post_parent' => $post->ID
       );
                                
       $hasChildQ = new WP_Query( $args );
                                
       if( !$hasChildQ->have_posts() ){ 
          get_template_part( 'content', 'recipe' ); 
       }
    endwhile; // end of the loop. 
?>

But I would like it to be done in the main query so that the paging will work with it.


Solution

  • I would suggest making that parent page a custom taxonomy. That way you could query by taxonomy.

    So your Recipe would be a CPT, but your recipe category would a 'Recipe Type' taxonomy.

    Online help:

    -d