1

I have everything working on my widget except the results! I created a taxonomy called country names that we use for several custom post types. I created this widget to display tour guides (CPT) from the selected country.

The widget is working properly on the back end, but I am getting no results on the front end (ie. "No listing found"). I have echoed the $instance to the screen and it is correct, so my args or query must be incorrect. Can anyone tell why?

Here is the code:

function widget($args, $instance) {  
extract( $args );   
$title = apply_filters('widget_title', $instance['title']);
$thiscountry = $instance['thiscountry'];
$before_widget = '<!-- '.$thiscountry.' -->';
echo $before_widget;
if ( $title ) {
    echo $before_title . $title . $after_title;
}
$this->getTourGuides($thiscountry);
echo $after_widget;
}

function getTourGuides($thiscountry) { //html
global $post;
$args = array(
'posts_per_page' => -1,
'post_type' => array( 'tourguides' ),
'tax_query' => array(
      array(
       'taxonomy'      => 'countryname',
       'field' => 'name', // also tried 'slug'
        'terms' => $thiscountry,
    ),    
  ),
);

//echo $thiscountry;
$listings = new WP_Query( $args);
if($listings->found_posts > 0) {
    echo '<ul class="guides_widget">';
        while ($listings->have_posts()) {
            /* Get custom post type fields */
            $ecpt_website = get_post_meta( get_the_ID(), 'ecpt_website', true );

            $listings->the_post();              
            $listItem = '<li>';
            $listItem .= '<a href="' . $ecpt_website . '">';
            $listItem .= get_the_title() . '</a>';
            $listItem .= '</li>';
            echo $listItem;
        } // end while
    echo '</ul>';
    wp_reset_postdata();
  }else{
    echo '<p style="padding:25px;">No listing found</p>';
  }
}

Code for the instance:

// constructor
function __construct() {
parent::__construct(
    'btn_tour_guides_by_country', // Base ID
    'BTN Tour Guides by Country', // Name
    array('description' => __( 'Display List of Tour Guides by Country in a Sidebar'))
   );
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['thiscountry'] = strip_tags($new_instance['thiscountry']);
return $instance;
}

// widget form creation
function form($instance) {
if( $instance) {
    $title = esc_attr($instance['title']);
    $thiscountry = esc_attr($instance['thiscountry']);
} else {
    $title = 'Tour Operators';
    $thiscountry = '';
}
?>
    <p>
    <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
    <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php 
echo $title; ?>" />
    </p>
    <p>
    <label for="<?php echo $this->get_field_id('thiscountry'); ?>"><?php _e('Country:'); ?></label>
    <select id="<?php echo $this->get_field_id('thiscountry'); ?>"  name="<?php echo $this->get_field_name('thiscountry'); ?>">
        <?php 
        $taxonomy = 'countryname';
        $queried_term = get_term_by( 'slug', get_query_var($taxonomy) );
        $terms = get_terms($taxonomy);
        //echo $term->slug;
        if ( $terms !== 0 ) {
            foreach ( $terms as $term ) {?> 
         <?php                     
            echo '<option '.selected($instance['thiscountry'], $term->name).' value="' . $term->name . '" id="' . $term->name . '">', $term->name, '</option>';     

               ?>

                  <?php                
            }
        }
         ?>

        <?php ?>
    </select>
    </p>

<?php
}
8
  • What's the code for selecting and saving $instance['thiscountry']? Sep 29, 2017 at 5:07
  • I added it above. The plugin is working fine in the backend and my echo seemd to be giving me the correct results. I just tried a print_r for $instance and found these two entries: Sep 29, 2017 at 5:16
  • the output works fine here with WP_Query args, maybe a spelling mistake on taxonomy or posttype? Sep 29, 2017 at 5:17
  • ooops, I hit enter. I am seeing [countryname] => (this is blank) and [thiscountry] => Bahamas ( which is correct for that page and sidebar). Sep 29, 2017 at 5:20
  • If I take out the tax_query I get the full list of tour guides for all countries, so it seems it has to be in the tax query Sep 29, 2017 at 5:25

1 Answer 1

0

I found the answer here. They were having the exact same problem: Using tax_query reverses my post_type argument in a custom WP_Query

It is a problem with the Easy Content Types plugin. Pippin, the author of this plugin wrote the following: The problem is caused by a function in the plugin that helps to resolve taxonomy archive 404s. It has a couple of issues, and because of them, there is a setting to disable the function. Go to Content Types > Settings and check "Disable Taxonomy Archive Fix". – Pippin May 11 '12 at 0:03

1
  • Ha! that fix didn't work - waiting to hear from support. Sep 30, 2017 at 1:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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