Wrong categories showing in directory page

Hi

I'm having an issue in that on this page; Link removed at members request which is a manually created page which uses this shortcode; [dr_list_categories style="grid"] the wrong categories are showing in the main part of the page.

I'd like this to show the categories of businesses, not the locations.

How can I fix this please?

Thanks

    Sally

    Thanks @Alexander Rohmann

    I've posted a couple of screenshots here to show you how I have my post types and Taxonomys set up.. have I done them correctly, or have I caused this error somehow?

    I've set them up this way in order to make the widgets on my homepage (bottom right side dropdowns for classifieds and directory) work correctly. But with one issue being solved I seem to have caused another.

    Any ideas would be much appreciated

    Thanks

    Sally

    Arnold

    Probem is you have multiple directory_listing categories. You have the built in "listing_category" and your own added "location" category. The plugin would only know about the categories it implements in general.

    In this case it does make anarray of all hierarchical taxonomies for directory_listing,
    and would display all the categories. both listing_category and location. Since it only displays the first X categories it's the luck of the sort as to which display first. In likely hood the oreder is by the taxonomy ID. and since listing_category was created first it has the lowest number and so that's what you get.

    You can modify theroutine to just use your category. In template-tags.php at about line 26 is the piece that gets the array of categories.
    $taxonomies = array_values(get_taxonomies(array('object_type' => array('directory_listing'), 'hierarchical' => 1)));

    Just add a line below it to force it to your category.

    $taxonomies = 'location';

    and you'll just get yoru locations.

    Sally

    Brilliant, thanks @Arnold - works a treat

    One last question, can you just confirm how I do this with my child theme. At the moment I've modified the parent plugin (as that's the only one I have at the mo) but I know this is wrong because as soon as the directory plugin gets updated i'll loose these settings.

    So what exactly do I do to get this in a child plugin if that makes sense?

    Do I copy absolutely everything out of that template-tags.php file and make a duplicate in my bb-fun-child folder (and do I first make another folder within there called directory.php? or something else)

    Don't want to do it wrong

    Arnold

    Similar function in Classifieds is in functions.php.

    There is a filter in get_categories() that you can add a filter to and change the taxonomies. So you wouldn't change the taxonomies sent to get_categories but would filter them internally.

    The filter is 'get_categories_taxonomy' The filter is called like this:

    $taxonomy = apply_filters( 'get_categories_taxonomy', $taxonomy, $args );

    So you would write a filter function that would receive the $taxonomy. If it's equal to your two categories change it to just your one category and return that. Then use add_filter() to hook it up in your child functions.php file

    Sally

    @Arnold

    Hi, I'm still struggling with this, but firstly, with the first issue we solved (the directory categories fixed by doing this);

    You can modify theroutine to just use your category. In template-tags.php at about line 26 is the piece that gets the array of categories.
    $taxonomies = array_values(get_taxonomies(array('object_type' => array('directory_listing'), 'hierarchical' => 1)));

    Just add a line below it to force it to your category.
    $taxonomies = 'location';
    and you'll just get yoru locations.

    Please can you tell me how I can protect those changes so that I can install the Directory update safely?

    Thanks

    Sally

    Sally

    Sorry @Arnold, this is all going way over my head But I'm trying...

    Is Editing classifieds/core/functions.php (inactive) the correct file I'm trying to change to get the classifieds sorted?

    If so, I can't see `
    $taxonomy = apply_filters( 'get_categories_taxonomy',` anywhere in there?

    I could just be blind, or I could be in completely the wrong place?

    When I find it, am I adding `
    $taxonomies = 'classifieds_categories';` beneath it?

    Confused!

    Alexander

    Hi @Sally,

    You wouldn't edit the core files. That's the purpose of having a filter. In another plugin, or perhaps your theme's functions.php file as Arnold mentioned, you can make use of any filter available for example:

    function my_custom_taxonomy_function($taxonomy,$args) {
    
        if ($taxonomy == 'something')
            $taxonomy = 'something_else';
    
        return $taxonomy;
    }
    
    add_filter('get_categories_taxonomy','my_custom_taxonomy_function',10,2);

    This is more of an example of how the hooks would work, I'm not sure if this is exactly the code you need for this scenario.

    Just be sure to return $taxonomy from the function so it passes through the filter. For more on using hooks, take a look at this article: http://codex.wordpress.org/Plugin_API/Action_Reference

    Best regards,

    Sally

    Hi @Alexander Rohmann @Arnold

    I'm still working on trying to fix this, if you can help me?

    Jack gave me this code;

    function my_custom_tax() {
    	global $post;
    
    	$terms = wp_get_post_terms( $post->ID, 'taxonomy_name' );
    
    	foreach ( $terms as $term ) {
    		// Get a single term from the array of terms.
    		if ( !isset( $single_term ) )
    			$single_term = $term['term_id'];
    	}
    
    	return $single_term;
    }
    add_filter( 'get_categories_taxonomy', 'my_custom_tax'  );
    ?>

    to insert into a plugin which I made myself following his instructions. However, when I got the plugin working, it resulted in both the classifieds and the directory listings shortcodes returning this on the front end;

    Catchable fatal error: Object of class WP_Error could not be converted to string in /home/rrokngem/public_html/thehouseofdog.co.uk/wp-content/plugins/classifieds/core/functions.php on line 44

    Firstly, do I need to use a plugin for this? Can I not simply add the code Jack gave me to the child themes function.php?

    Secondly, can you see anything wrong with the code above?

    Why does this affect both the classifieds and directory shortcodes?

    The only taxonomy I want to display on the classifieds shortcode is classifieds_categories and the only one I want to display on the directory shortcode is listing_category

    Neither of these appear in that code Jack gave me, so I can't see how it knows what I mean?

    The directory shortcode was fixed by the method @Arnold recommended at the start of this topic quite simply, but I'm just a bit concerned that this will be overwritten every time I update the directory plugin. Can you also tell me how I can sort the directory shortcode out so it doesn't get wiped out when I update?

    Thanks

    Sally

    Arnold

    Kind of overkill. It's a Filter so the taxonomy array is passed in to the function for you. The signature is

    $taxonomy = apply_filters( 'get_categories_taxonomy', $taxonomy, $args );

    so simple check if it's there. Also your code does nothing to chose between one taxonomy or another. Your $single_term is undefined so has does isset work?
    I'd just test for the particular taxonomy I'm interested in.

    function my_custom_tax($taxonomy='', $args='') {
    
    // cast $taxonomy as an array so in_array() always works.
      if( in_array('location', (array) $taxonomy)  ) {
        return array('location');
      }
      return $taxonomy; //Wasn't us so pass original.
    }
    add_filter( 'get_categories_taxonomy', 'my_custom_tax'  );
    ?>

    Put it in your child themes functions.php

    Arnold

    Kind of overkill. It's a Filter so the taxonomy array is passed in to the function for you. The signature is

    $taxonomy = apply_filters( 'get_categories_taxonomy', $taxonomy, $args );

    so simple check if it's there. Also your code does nothing to chose between one taxonomy or another. Your $single_term is undefined so has does isset work?
    I'd just test for the particular taxonomy I'm interested in.

    function my_custom_tax($taxonomy='', $args='') {
    
    // cast $taxonomy as an array so in_array() always works.
      if( in_array('location', (array) $taxonomy)  ) {
        return array('location');
      }
      return $taxonomy; //Wasn't us so pass original.
    }
    add_filter( 'get_categories_taxonomy', 'my_custom_tax'  );
    ?>

    Put it in your child themes functions.php

    Sally

    Hi @Arnold

    I added

    function my_custom_tax($taxonomy='', $args='') {
    
    // cast $taxonomy as an array so in_array() always works.
      if( in_array('location', (array) $taxonomy)  ) {
        return array('location');
      }
      return $taxonomy; //Wasn't us so pass original.
    }
    add_filter( 'get_categories_taxonomy', 'my_custom_tax'  );
    ?>

    to my child themes functions.php now, but nothing's changed.

    Please can you explain (in idiots terms!) what I do with

    $taxonomy = apply_filters( 'get_categories_taxonomy', $taxonomy, $args );

    To make just the classifieds_categories taxonomy appear on the classifieds page at Link removed at members request

    and

    just the listing_category taxonomy appear on the directory page at Link removed at members request

    Because I am aware that the fix I did for the directory page is not sufficient as it will be overwritten as soon as I update the plugin.

    Sorry if I keep repeating things, I just really don't know how I tell each separate plugin what to do and stay that way

    Thanks

    Sally