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 );