I am trying to modify the number of posts per page for archive pages that contain category sticky posts (added by the Category Sticky post plugin)
to do so I am using the pre_get_posts
hook and have tried the following :
add_action( 'pre_get_posts', 'my_set_category_posts_per_page' );
function my_set_category_posts_per_page( $query ) {
global $wp_the_query;
if ( 'category_sticky_post' === $query->get( 'meta_key' ) && ! is_paged ) {
$query->set( 'posts_per_page', 8 );
}
return $query;
}
I var_dump($query)
I do get [meta_key] => category_sticky_post
in the query_vars
but I can't sort out how to use it.
Another idea is to use something like this:
function my_set_category_posts_per_page( $query ) {
$args = array(
array(
'key' => 'category_sticky_post',
'compare' => 'EXISTS'
)
);
$meta_query = new WP_Meta_Query($args);
if ( 'category_sticky_post' === $query->get('meta_query', $meta_query) && ! is_paged() ) {
$query->set( 'posts_per_page', 8 );
}
}
How would I alter the query only when the meta key is present for the posts in the current query?
[meta_key] => category_sticky_post
nested under query_vars in the$query
object? I think your condition should look more like'category_sticky_post' === $query->query_vars['meta_key']
var_dump($query->query_vars['meta_key'])
I do getcategory_sticky_post
but however the condition doesn't return true.$query
object to Gist or Pastebin, etc? Can't guarantee a solution but I'm happy to take a deeper look.