This topic contains 9 replies, has 4 voices, and was last updated by  torarnv 2 years, 10 months ago.

  • Author
    Posts
  • #299

    I came across a REALLY weird bug today. The original issue is that any taxonomy used with a piklist-generated post type did not allow me to list out taxonomies with wp_tag_cloud().

    Through much troubleshooting with the queries generated by get_the_terms() and get_terms(), one of my developers found that the count in WP’s database is not being updated for these taxonomies.

    After doing a little searching, I found that posts that aren’t ‘published’ do not add to the taxonomy count. This includes custom post statuses created by Piklist. After removing the custom statuses and switching to original-recipe ‘published’, boom, all is well.

    This also may be related to something else I’ve noticed- custom post statuses don’t appear in the Quick Edit status menu.

    I may be able to provide more troubleshooting info if needed.

  • #300

    James Mc
    Participant

    @sarah: I’ve noticed that custom post statuses don’t appear in the quick edit status menu as well.

  • #672

    James Mc
    Participant

    I thought I would post in here instead of starting a new topic as it seems related…

    My question is:  If you have a CPT and a corresponding registered taxonomy do we need to, by default, implement the update_count_callback for the count of the CPT be displayed?”

    An example of how to do this can be found here.

  • #673

    James Mc
    Participant

    Just as a follow up, as discovered by Sarah, if you have a CPT with custom statuses, then you will need to implement an update_count_callback function as per my link above. If you do not, then your CPT count column will all be zeroes in your taxonomy types associated with the CPT. When I have a nice example, I will post it here…

  • #678

    Steve
    Keymaster

    @James_Mc– This is interesting.  Please let us know when you have a good example.

  • #684

    James Mc
    Participant

    I have updated the add-ons piklist demo example with an example of how to count CPT statuses, see here.

    When registering your taxonomy for your CPT, add your ‘update_count_callback’ function and then implement something similar to what I have done. In the example I provided for the piklist demo CPT (which has statuses of New, Demo and Lock), I am only counting posts of status ‘demo’.

    What would be even better is to have a way of defining an array of which statuses one considers ‘countable’ external to the callback function, so that the function can be more generalized.

    Anyway, hope this helps someone…

  • #688

    Steve
    Keymaster

    @james_MC–  This looks good.  Just curious though… why didn’t you use a slightly modified version of the default WordPress _update_post_term_count.

    If you remove the hard coded references to “publish” and pass an array of post statuses it will work as well.

  • #703

    James Mc
    Participant

    @Steve, I didn’t even think to refer to the WP core. I just found some examples online and combined and cleaned them up. I’ll take another swing at it and follow your examples of doing it ‘the WordPress way.’

  • #7436

    torarnv
    Participant

    Here’s a minimal trick to include the custom post statues in the count:

    // Ensure that custom post statues are also included in taxonomy counts
    function update_registration_term_count($terms, $taxonomy) {
    	function include_all_statuses($sql) {
    		return str_replace("post_status = 'publish'",
    			"post_status NOT IN ('inherit','auto-draft','trash')", $sql);
    	}
    	add_filter('query', 'include_all_statuses');
    	_update_post_term_count($terms, $taxonomy);
    	remove_filter('query', 'include_all_statuses');
    }
  • #7437

    torarnv
    Participant

    Apologies, should be:

    // Ensure that custom post statues are also included in taxonomy counts
    function update_foo_term_count($terms, $taxonomy) {
    	$include_all_statuses = function($sql) {
    		return str_replace("post_status = 'publish'",
    			"post_status NOT IN ('inherit','auto-draft','trash')", $sql);
    	};
    	add_filter('query', $include_all_statuses);
    	_update_post_term_count($terms, $taxonomy);
    	remove_filter('query', $include_all_statuses);
    }
    

You must be logged in to reply to this topic.