admin管理员组

文章数量:1122846

Currently, I have a query that searches for all employees with a taxonomy term that matches the word that is searched for in an input field. That works perfectly fine but my client now wants the employee results to be displayed in alphabetical order by last name which is a custom field that I created in the admin panel using Toolset Types.

Whenever I try to add

'meta_key' => 'wpcf-last-name',
'orderby' => 'meta_value',
'order' => 'DESC'

to the list of arguments, it breaks the whole query.

Here is the working code below:

$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 );

Currently, I have a query that searches for all employees with a taxonomy term that matches the word that is searched for in an input field. That works perfectly fine but my client now wants the employee results to be displayed in alphabetical order by last name which is a custom field that I created in the admin panel using Toolset Types.

Whenever I try to add

'meta_key' => 'wpcf-last-name',
'orderby' => 'meta_value',
'order' => 'DESC'

to the list of arguments, it breaks the whole query.

Here is the working code below:

$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 );
Share Improve this question asked Dec 13, 2017 at 20:10 Vanessa CeballosVanessa Ceballos 1
Add a comment  | 

1 Answer 1

Reset to default 0

You can use a meta_query in addition to your tax_query : https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters

I have in which if statement you want to use it so I put it in the of the code.

$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 );

Hope it helps :)

本文标签: Order Wordpress Query by Custom Field While Still Using Taxquery Argument