0

I have a custom post type called "galleries," which is hierarchical.

I'm using a function in my functions.php file to modify the number of posts displayed per page to 48. That part works fine, but I also need it to exclude all child posts, so that only top-level parent posts make up the 48 posts pulled in per page. Here is the current code:

    function projects_custom_number_of_posts( $query ) {
    if ( is_admin() || ! $query->is_main_query() )
        return;

    if ( is_post_type_archive( 'galleries' ) ) {
        // Display 48 posts for a custom post type called 'galleries'
        $query->set( 'posts_per_page', 48 );
        return;
    }
}
add_action( 'pre_get_posts', 'projects_custom_number_of_posts', 1 );

How can I modify the $query->set component to also limit the posts to top-level parent posts only? I tried the following, but it throws an error:

    function projects_custom_number_of_posts( $query ) {
    if ( is_admin() || ! $query->is_main_query() )
        return;

    if ( is_post_type_archive( 'galleries' ) ) {
        // Display 48 posts for a custom post type called 'galleries'
        $query->set( array ('posts_per_page'=>48, 'post_parent'=>0) );
        return;
    }
}
add_action( 'pre_get_posts', 'projects_custom_number_of_posts', 1 );
3

Just call $query->set 2 times:

$query->set('posts_per_page', 48 );
$query->set('post_parent', 0 );
  • That's right! I just figured it out on my own right after you posted. Always the most obvious solution that I overlook... – Evster Oct 8 '13 at 23:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.