Wordpress:pre_get_posts和按分类学术语排序

时间:2013-02-13 14:04:09

标签: wordpress custom-taxonomy

我有一个带有自定义分类“客户”的类别存档。现在,我想按自定义分类(客户名称)的术语订购类别归档。我尝试通过meta_value和高级自定义分类查询来查询它,但我无法通过分类术语的名称对其进行排序。有任何建议来获得按分类学术语名称排序的Wordpress循环吗?见行$ query-> set('orderby','how_to_order_by_taxonomy_name');

注册分类

add_action( 'init', 'jp_register_project_taxonomies', 0 );
function jp_register_project_taxonomies() {

    // register taxonomy to hold our clients
    register_taxonomy(
        'client',
        array( 'post' ),
        array(
            'hierarchical' => false,
            'public' => true,
            'query_var' => true,
            'rewrite' => true,
            'labels' => array(
                'name'                => _x( 'Clients', 'taxonomy general name' ),
                'singular_name'       => _x( 'Client', 'taxonomy singular name' ),
                'search_items'        => __( 'Search Clients' ),
                'all_items'           => __( 'All Clients' ),
                'edit_item'           => __( 'Edit Client' ), 
                'update_item'         => __( 'Update Client' ),
                'add_new_item'        => __( 'Add New Client' ),
                'new_item_name'       => __( 'New Client Name' ),
                'menu_name'           => __( 'Client' )
            ),
        )
    );
}

pre_get_posts查询

add_action( 'pre_get_posts', 'jp_project_taxonomy_queries' );

function jp_project_taxonomy_queries( $query ) {

    if(!is_admin() && $query->is_main_query() && is_category() ):

        if (get_query_var('poby') == 'client'):

            $taxonomies = array();
            $tax_order = (get_query_var('po') == 'DESC' ?  'DESC' : 'ASC' );

            foreach (get_terms('client', array('order' => $tax_order)) as $tax ) {
                $taxonomies[] = $tax->name;
            }

            $taxquery = array(
                array(
                    'taxonomy' => 'client',
                    'terms' => $taxonomies,
                    'field' => 'slug',
                )
            );

            $query->set( 'tax_query', $taxquery );
            $query->set( 'orderby', 'how_to_order_by_taxonomy_name' );

        endif;

    endif;

}

1 个答案:

答案 0 :(得分:0)

据我所知,there is no parameter to order a WP_Query by term

我对这个问题的解决方案通常是创建一个包含术语名称或术语 slug 的元字段。此字段是在使用钩子 save_postsave_post_{post_type} 保存帖子时创建的。

例如,要按出版年份(分类日期)对书籍列表(post type book)进行排序,我们可以使用此函数来创建元字段:

add_action( 'save_post_book', 'prefix_save_date_as_meta', 10 );
function prefix_save_date_as_meta ($post_id) { 
    $years = get_the_terms($post_id,'date');

    if( empty($years) )
        return;

    $years_list = wp_list_pluck($years,'name');

    update_post_meta($post_id,'_book_date',$years_list[0]);

    return;
}

还有另一个用于对查询进行排序的函数:

add_filter( 'pre_get_posts', 'prefix_custom_args_for_loops' );
function prefix_custom_args_for_loops( $query ) {
    if ( !is_admin() && is_post_type_archive('book') && $query->is_main_query() ) {
        $query->set( 'orderby','meta_value_num');
        $query->set( 'meta_key','_book_date');
    }
    return $query;
}