admin管理员组文章数量:1418607
I want to get the current post type inside functions.php
This is a function to sort reviews by custom meta key. I've managed to get the reviews sorted using the following code:
// Sort reviews in category page by customers rating - START
function pre_get_posts_hook($wp_query) {
if( is_admin() ) {
return $wp_query;
}
if ( $wp_query->is_main_query() && ( is_category() || is_archive() ) )
{
$wp_query->set( 'orderby', 'meta_value' );
$wp_query->set( 'meta_key', 'user_average' );
$wp_query->set( 'order', 'DESC' );
return $wp_query;
}
}
add_filter('pre_get_posts', 'pre_get_posts_hook' );
// Sort reviews in category page by customers rating - END
But, when I add && $wp_query->is_post_type_archive('review')
in order to check if the current post type is 'review' before sorting, the entire function does not seem to work!
// Sort reviews in category page by customers rating - START
function pre_get_posts_hook($wp_query) {
if( is_admin() ) {
return $wp_query;
}
if ( $wp_query->is_main_query() && ( is_category() || is_archive() ) && $wp_query->is_post_type_archive('review') )
{
$wp_query->set( 'orderby', 'meta_value' );
$wp_query->set( 'meta_key', 'user_average' );
$wp_query->set( 'order', 'DESC' );
return $wp_query;
}
}
add_filter('pre_get_posts', 'pre_get_posts_hook' );
// Sort reviews in category page by customers rating - END
Any idea on how to fix this problem?
Thanks in advance.
本文标签: functionsHow to get current post type
版权声明:本文标题:functions - How to get current post type? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745289682a2651715.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论