在仍使用TAX_QUERY参数的情况下按自定义字段排序WordPress查询

时间:2017-12-13 作者:Vanessa Ceballos

目前,我有一个查询,用于搜索所有分类术语与在输入字段中搜索的单词相匹配的员工。这很好,但我的客户现在希望员工结果按姓氏的字母顺序显示,姓氏是我在管理面板中使用工具集类型创建的自定义字段。

每当我尝试添加

\'meta_key\' => \'wpcf-last-name\',
\'orderby\' => \'meta_value\',
\'order\' => \'DESC\'
对于参数列表,它会打断整个查询。

以下是工作代码:

$names = array(
    \'fields\' => \'names\'            
);
$prac_names = get_terms(\'attorney-practice\', $names);
$prac_matches  = array_shift(preg_grep (\'(\'. $search .\')\', $prac_names));

$off_names = get_terms(\'office-location\', $names);
$off_matches  = array_shift(preg_grep(\'(\'. $search .\')\', $off_names));



    if(in_array($prac_matches, $prac_names)) {
        $args = array(
            \'post_type\' => \'employee\',
            \'tax_query\' => array(
                \'relation\' => \'OR\',
                array(
                    \'taxonomy\' => \'attorney-practice\',
                    \'field\'    => \'name\',
                    \'terms\'    => $prac_matches
                )
            )
        );
    }elseif(in_array($off_matches, $off_names)) {
        $args = array(
            \'post_type\' => \'employee\',
            \'tax_query\' => array(
                \'relation\' => \'OR\',
                array(
                    \'taxonomy\' => \'office-location\',
                    \'field\'    => \'name\',
                    \'terms\'    => $off_matches
                )
            )
        );
    }


    $query = new WP_Query( $args );

1 个回复
SO网友:Elex

您可以使用meta_query 除了您的tax\\u查询之外:https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

我有一个if 语句您想使用它,所以我将它放在代码的。

$names = array(
    \'fields\' => \'names\'            
);
$prac_names = get_terms(\'attorney-practice\', $names);
$prac_matches  = array_shift(preg_grep (\'(\'. $search .\')\', $prac_names));

$off_names = get_terms(\'office-location\', $names);
$off_matches  = array_shift(preg_grep(\'(\'. $search .\')\', $off_names));



if(in_array($prac_matches, $prac_names)) {
    $args = array(
        \'post_type\' => \'employee\',
        \'tax_query\' => array(
            \'relation\' => \'OR\',
            array(
                \'taxonomy\' => \'attorney-practice\',
                \'field\'    => \'name\',
                \'terms\'    => $prac_matches
            )
        )
    );
}elseif(in_array($off_matches, $off_names)) {
    $args = array(
        \'post_type\' => \'employee\',
        \'tax_query\' => array(
            \'relation\' => \'OR\',
            array(
                \'taxonomy\' => \'office-location\',
                \'field\'    => \'name\',
                \'terms\'    => $off_matches
            )
        )
    );
}

// Here you add your meta_query to get the value from post_meta
$args[\'meta_query\'] => array(
    array(
        \'key\'     => \'wpcf-last-name\',
        \'value\'   => \'YOUR VALUE\',
        \'compare\' => \'LIKE\',
    ),
);

// Then you set the order with your 
$args[\'orderby\'] => \'meta_value\',
$args[\'order\'] => \'DESC\'

$query = new WP_Query( $args );
希望有帮助:)

结束