I have a directory plugin with a custom taxonomy for locations that includes following levels country>state>city . I have listings that are statewide and nationwide that I want shown if the city is selected in the filter, or said differently the statewide have empty cities but a selected state and the nationwide have a selected country but empty state and cities. I have also tried using a separate field to check off nationwide or statewide. So, when someone filters for a city, I want the nationwide to show up and the statewide to show up IF it is within the respective state.

The original plugin code is as follows:

` <?php if ($args['show_where_search']): ?>
    <div class="w2dc-row">
        <?php if (($args['show_locations_search'] && w2dc_is_anyone_in_taxonomy(W2DC_LOCATIONS_TAX)) && $args['show_address_search']) $col_md = 6; else $col_md = 12; ?>
        <?php if ($columns == 1) $col_md = 12; ?>
        <div class="w2dc-search-section-label w2dc-col-md-12"><?php _e('Search near:', 'W2DC'); ?></div>
        <?php if ($args['show_locations_search'] && w2dc_is_anyone_in_taxonomy(W2DC_LOCATIONS_TAX)): ?>
        <?php
        if (get_query_var('location-w2dc') && ($location_object = w2dc_get_term_by_path(get_query_var('location-w2dc'))))
            $term_id = $location_object->term_id;
        elseif (isset($_GET['location_id']) && is_numeric($_GET['location_id']))
            $term_id = $_GET['location_id'];
        else 
            $term_id = w2dc_getValue($args, 'location');; ?>
        <div class="w2dc-col-md-<?php echo $col_md; ?>">
            <?php w2dc_tax_dropdowns_init(W2DC_LOCATIONS_TAX, 'location_id', $term_id, get_option('w2dc_show_location_count_in_search'), array(), $w2dc_instance->locations_levels->getSelectionsArray(), null, $args['exact_locations']); ?>
        </div>
        <?php endif; ?>`

I added a checkbox that says ‘virtual’ to test to see if I could have those that are statewide show up and have tried this:

add_filter('pre_get_posts','add_statewide_orgs');
function add_statewide_orgs(&$wp_query) {
if (!get_query_var('location-w2dc')) return;
$requested_area = get_query_var(‘location-w2dc');
$tax_query = array(
           array(
             'taxonomy'=>'location-w2dc',
             'field'=>'slug',
             'terms'=>array($requested_area),
             'operator'=>'IN'
           ),
           array(
             'taxonomy'=>'location-w2dc',
             'field'=>'slug',
             'terms'=>array(‘virtual'),
             'operator'=>'IN', 
             <strong>'include_children'=>0</strong>
           ),
           'relation'=>'OR'
         );
$wp_query->set('tax_query',$tax_query);
}

It didn’t work, but I am trying to find the solution for having either those nationwide and statewide show up by allowing those with empty child categories show up for the parent level OR by checking a box to denote if they are statewide and nationwide. Though, for statewide, they’d need to show up for their respective state.

I also have the Relevanssi plugin but was not sure if there was a filter hook or something that could be done using that plugin. I am open to any possible solution. THANKS!

Read more here: How to show the parent posts with an empty child if the custom taxonomy filter has the child selected?


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: