functions - How to get current post type?

I want to get the current post type inside functions.phpThis is a function to sort reviews by custom meta key. I've

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745289683a4620779.html

相关推荐

  • functions - How to get current post type?

    I want to get the current post type inside functions.phpThis is a function to sort reviews by custom meta key. I've

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信