admin管理员组文章数量:1122826
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?
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?
Share Improve this question edited Oct 18, 2016 at 16:17 mantis asked Oct 18, 2016 at 12:56 mantismantis 7902 gold badges18 silver badges38 bronze badges 6 | Show 1 more comment1 Answer
Reset to default 0Based on data from your Gist, the following pre_get_posts
condition should work to limit your posts per page.
add_action( 'pre_get_posts', 'my_set_category_posts_per_page' );
function my_set_category_posts_per_page() {
global $wp_query;
if ( 'category_sticky_post' === $wp_query->query_vars['meta_key'] ) {
$wp_query->set( 'posts_per_page', 8 );
}
}
本文标签: loopAlter query using pregetposts() if metakey is present
版权声明:本文标题:loop - Alter query using pre_get_posts() if meta_key is present 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736288406a1928088.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
[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']
– jdm2112 Commented Oct 18, 2016 at 18:26var_dump($query->query_vars['meta_key'])
I do getcategory_sticky_post
but however the condition doesn't return true. – mantis Commented Oct 19, 2016 at 8:09$query
object to Gist or Pastebin, etc? Can't guarantee a solution but I'm happy to take a deeper look. – jdm2112 Commented Oct 19, 2016 at 14:20