0

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

  • Hi @jean-rémi larcelet-prost , Thank you for the response. I tried implementing the code you provided and unfortunately it didn't work. The only issue I can think of is that the meta_query requires a value and in my case it needs to be a dynamic value. The result of the query needs to show any employee ordered by the last name value but this can be many things depending on which taxonomy term is matches with the employee.Is there anyway that you know of to get around this? – Vanessa Ceballos Dec 14 '17 at 18:50
  • Is your value a number ? Or a string ? – Elex Dec 14 '17 at 18:53
  • It's a string such as the last name of a person – Vanessa Ceballos Dec 14 '17 at 19:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.