1

I have created custom taxonomies for a custom post type. I have added drop downs to the search form so I can add the custom taxonomies to the search criteria.

The problem I am having is that when my query includes a particular taxonomies, I get no returns, even though I should. When I check the SQL query, it is inserting 0 = 1 which usually means that it can't find any results for the taxonomy (or there is a problem with the tax_query part of the overall query).

If I simply create a custom query (WP_Query) using the same tax_query parameters, I get results. So I'm trying to figure out why it doesn't work in the Search context but does work in a custom query.

Here is the custom taxonomy as I've created it:

$labels = array(
    'name'              => __( 'Sex', 'hrd_record' ),
    'singular_name'     => __( 'Sex', 'hrd_record' ),
    'search_items'      => __( 'Search Sexes', 'hrd_record' ),
    'all_items'         => __( 'All Sexes', 'hrd_record' ),
    'parent_item'       => __( 'Parent Sex', 'hrd_record' ),
    'parent_item_colon' => __( 'Parent Sex:', 'hrd_record' ),
    'edit_item'         => __( 'Edit Sex', 'hrd_record' ),
    'update_item'       => __( 'Update Sex', 'hrd_record' ),
    'add_new_item'      => __( 'Add New Sex', 'hrd_record' ),
    'new_item_name'     => __( 'New Sex Name', 'hrd_record' ),
    'menu_name'         => __( 'Sexes', 'hrd_record' ),
    );

    $args = array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'sex' ),
    );

    register_taxonomy( 'sex', '', $args );
    register_taxonomy_for_object_type( 'sex', 'hrdrecord' );

My search form includes other taxonomies and here is the code I am using which is added to the filter 'pre_get_posts':

if ($query->is_search) {

$tax_query = array();
$meta_query = array();


 $query->set('post_type','hrdrecord');

 $region = (isset($_GET['region'])) ? (int)$_GET['region'] : '';
 $country = (isset($_GET['country'])) ? (int)$_GET['country'] : '';
 $state = (isset($_GET['state'])) ? (int)$_GET['state'] : '';

 $sex = (isset($_GET['sex'])) ? (int)$_GET['sex'] : '';
 $sector = (isset($_GET['sector'])) ? $_GET['sector'] : '';
 $sector_detail = (isset($_GET['sector_detail'])) ? $_GET['sector_detail'] : '';

 $nickname = (isset($_GET['nickname'])) ? $_GET['nickname'] : '';

  if (!empty($nickname)) {
    $meta_query = array(
        array(
        'meta_key'     => '_nickname',
        'meta_value'   => $nickname,
        'meta_compare' => 'LIKE'
        ),
    );
  }


  if (!empty($state)) {
    $location = $state;
  } elseif (!empty($country) && empty($state)) {
    $location = $country;
  } elseif (!empty($region) && empty($country) & empty($state)) {
    $location = $region;   
  }

  $tax_query = array( 'relation' => 'AND', );

  if (!empty($location)) {  //Location

    array_push($tax_query, array('taxonomy' => 'location',
            'field' => 'term_id',
            'terms' => $location,
            'include_children' => false,
            ));

  } 

  if (!empty($sex)) {  //Sex

    array_push($tax_query, array('taxonomy' => 'sex',
            'field' => 'term_id',
            'terms' => $sex,
            ));

  } 

  if (!empty($sector)) { //Sector

    array_push($tax_query, array('taxonomy' => 'sector',
            'field' => 'slug',
            'terms' => $sector,
            ));

  } 

  if (!empty($sector_detail)) { //Sector Detail

    array_push($tax_query, array('taxonomy' => 'sector-detail',
            'field' => 'slug',
            'terms' => $sector_detail,
            ));

  } 

if (!empty($tax_query)) {
    $query->set( 'tax_query', $tax_query );
}

if (!empty($meta_query)) {
    $query->set( 'meta_query', $meta_query );
}

}

return $query;

So if I have values for any of the other taxonomies or a combination of them...I get results but the minute I add a value for the 'sex' taxonomy, there are no results...even though the same query done using WP_Query will return results.

In fact my fix for this is to add this custom theory in my search.php template in the event I have no results (I check if sex was not empty in the query string and then run my custom theory---but I'd rather sort out why it's not picking it up in the first instance)

Your Answer

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

Browse other questions tagged or ask your own question.