• Resolved tosca30

    (@tosca30)


    The question is not strictly related to MLA, but I ask nonetheless hoping you’ve encountered this problem in MLA context and have an idea to solve the issue.

    For hierarchical custom taxonomies, I would like to display pad_counts, that is the number of media linked to a parent term and its children.
    Ideally, these would be seen:
    – on the index page of my site
    – and in multi-criteria search form that shows in the sidebar.

    Whatever I do, the pad_counts only display the count for the actual term, not including its children.

    Can you provide any hint?
    Thanks a lot.

    https://wordpress.org/plugins/media-library-assistant/

Viewing 11 replies - 1 through 11 (of 11 total)
  • Plugin Author David Lingren

    (@dglingren)

    I would like a bit more information so I can give you the most helpful answer. Can you tell me how you generate the list of terms on your index page, and in the dropdown lists in the sidebar? What have you tried that is not working?

    I have looked at some of the WordPress code that implements the terms list and it has special behavior for pad_counts. If you are using WordPress functions it would be helpful to know which function(s) and what arguments you are using.

    In the current MLA version the [mla_tag_cloud] shortcode does not support pad_counts because it does not use the WordPress functions to generate the list.

    Any additional information you can give me would be very helpful.

    Thread Starter tosca30

    (@tosca30)

    Thanks for willing to help.

    The index page uses the wp_list_categories function with the following arguments:

    'hierarchical'	=> 1,
    'hide_empty'	=> 0,
    'pad_counts'	=> 1,

    for each taxonomy.
    Currently, the show_count parameter is set to false in order not to show wrong (= non including children’s) counts.

    And the dropdown lists use the get_terms function with the same arguments … and same result.

    I’ve tried every combination of hierarchical, hide_empty, pad_counts and show_count arguments, but have never be able to display the right pad_counts.

    Plugin Author David Lingren

    (@dglingren)

    I believe the WordPress wp_list_categories and get_terms functions rely on a “count” column in the term_taxonomy database table to compute the pad_count values. This column does not always have accurate counts for attachments/media library items. That is why MLA provides a replacement “Attachments” column in the Media/Att. Categories submenu.

    You can use the Attachments column to look at a few of your terms and see if the counts add up with and without the pad_counts argument. You may find that the counts are not always right in either case.

    I am traveling until June 10, but I can do more investigation when I am back home. Let me know if you can compare the “Attachments” column values with the get_terms values. Thanks for your understanding and your patience.

    Thread Starter tosca30

    (@tosca30)

    Thank you for investigating.

    I forgot to tell that I use the _update_generic_term_count option (see note on this page) in order to get the right count for media.

    From what I can see the count column is consistent with the number of media linked to each term, that is not including children term; and that is what I see in both the admin pages and in front pages when I activate the show_count parameter in get_terms or wp_list_categories function. But the aggregation never takes place.

    I and will have only occasionnally access to internet during the next 3 weeks.

    Plugin Author David Lingren

    (@dglingren)

    Thank you for the update and the link to the Codex page. Thanks as well for confirming that the counts associated with your terms are correct.

    I will dig deeper into this issue when I return from my trip, and I will post any progress here.

    Thanks again for your patience.

    Plugin Author David Lingren

    (@dglingren)

    Thanks again for the link to the _update_generic_term_count notes. I am going to add this parameter to the MLA attachment_category and attachment_tag taxonomies as default settings.

    I have done some digging into the WordPress source code for get_terms, which is also used by wp_list_categories. I regret that the news is not good.

    The get_terms function calls an internal supporting function, _pad_term_counts, when the pad_counts=true parameter is given as an argument. This function ignores the counts in the terms database table; it does a separate SQL query to get a list of the post IDs to which the terms are assigned. The query includes post_status = 'publish' as one of the selection clauses. Since attachments always have post_status = 'inherit', they are never included in the results of the query and therefore the counts are returned unchanged; no padding.

    I modified the WordPress code to remove this clause and got the desired results. I suppose this could be considered a WordPress bug, and I will report it as such. I wouldn’t wait for a fix, however.

    I will do a bit more work and see if I can come up with an alternative function you can use in your PHP code to get the results you want. I am interested in adding something like this to the [mla_tag_cloud] shortcode in a future release as well.

    Thanks for raising an interesting issue and for your patience while I got to the root cause. I will leave this topic unresolved for now and post an update when I work out an alternative function.

    Plugin Author David Lingren

    (@dglingren)

    Thanks for your help and your patience while I worked on the “pad_counts” issue you identified. I have made some enhancements to the [mla_tag_cloud] shortcode and its supporting MLAShortcodes::mla_get_terms( $attr ) function to address the problem. In particular, I have added post_type, post_status and pad_counts parameters.

    As I posted above, the database counts cannot be used for pad_counts because they contain the total count for each term. If a post/page/attachment has been assigned to both a parent and a child term, e.g., parent “flora”/”landscape” and child “flower”/”mountains”, it will be counted in both terms’ database counts. However, the pad_counts result should only count it once and thus a separate SQL query is required.

    The database counts also have some other special logic, such as counting attachments only if they are actually attached to a published parent post/page (but not when _update_generic_term_count is used).

    The post_type field can have many values (post, page, attachment, revision, nav_menu_item and any registered custom post type). The MLA shortcode and supporting function allows all type values, e.g., “attachment” for attachments.

    The post_status field can have many values (publish, future, draft, pending, private, trash, auto-draft, inherit). By default, WordPress only allows publish status. The MLA shortcode and supporting function allows all status values, e.g., “inherit” for attachments and “private” when the user is logged in.

    For taxonomies such as attachment_category and attachment_tag you want to count post_type=attachment and post_status=inherit; these are the MLA defaults. For the WordPress category and post_tag taxonomies you would normally want post_type=post and post_status=publish or post_status="publish,private".

    If you use a taxonomy such as category or post_tag for pages and/or attachments, the parameter values depend on what you want to count. To get a complete count of all objects assigned to the terms you would code: post_type="post,page,attachment and post_status="publish,private,inherit". Both type and status values must be considered to get the count you want.

    The new parameters can be added to the [mla_tag_cloud] shortcode, and you can use the supporting MLAShortcodes::mla_get_terms( $attr ) function from your PHP code as a substitute for the WordPress get_terms() function. For your site, I believe the appropriate parameters would be:

    $attr = array (
      'taxonomy' => 'category',
      'post_mime_type' => 'all',
      'post_type' => 'attachment',
      'post_status' => 'inherit',
      'pad_counts' => 'true',
      'limit' => 0,
      'offset' => 0,
    );

    Most of the above parameters are actually the default values, so you could also code:

    $attr = array (
      'taxonomy' => 'category',
      'pad_counts' => 'true',
    );

    Calling the function would be coded as:

    $terms = MLAShortcodes::mla_get_terms( $attr );

    The function would return something like:

    $terms = array (
      0 =>
      stdClass::__set_state(array(
         'term_id' => '5',
         'name' => 'General Cat',
         'slug' => 'general',
         'term_group' => '0',
         'term_taxonomy_id' => '5',
         'taxonomy' => 'category',
         'description' => '',
         'parent' => '0',
         'count' => 14,
      )),
      'found_rows' => 1,
    )

    Note that in addition to the term(s) that match your criteria, the function returns one additional array element; found_rows with the total count of terms. This can be more that the terms returned in the array if you have used the limit and/or offset parameters to get partial results. You probably want to remove this element before using the results in a foreach ( $terms as $term ) loop; unset( $terms['found_rows'] ); will do the trick.

    I hope that answers your question and gives you a solution for your site. Please update this topic if you have any problems or further questions about this issue.

    The new parameters will be available in the next MLA version. I will leave this topic unresolved until the next version goes out. Thanks for bringing this to my attention and for motivating an improvement to the plugin.

    Plugin Author David Lingren

    (@dglingren)

    I have released MLA v1.90, which includes the new post_status, post_type and pad_counts parameters for the [mla_tag_cloud] shortcode. I believe these new parameters will let you get the counts you need for your site.

    I am marking this topic resolved, but please update it if you have any questions about or problems with the new parameters. Thanks for motivating an enhancement to the plugin.

    Thread Starter tosca30

    (@tosca30)

    Sorry for not having come back sooner, but I had to develop/launch another website in the mean time.

    Thank you very much for all the tremendous work and the explanations on this issue.

    Your solution works perfectly fine to replace the ordinary get_terms() function, as in my sidebar search boxes.

    However, I don’t see how to use your custom function for my index page, that has to display the whole category tree, and currently uses the wp_list_categories function for this purpose.

    Have you any idea how I can achieve this?

    Plugin Author David Lingren

    (@dglingren)

    Thank you for your update and the good news about the progress you’ve made. I believe there’s an easy solution to your wp_list_categories issue as well.

    The wp_list_categories function accepts a walker parameter that allows you to extend its functionality. You can use this to substitute the accurate, padded count of media items for each term in the whole category tree.

    First, add this PHP Class definition to your page template:

    /**
     * Class MNA_Pad_Counts_Walker adds accurate, padded attachment counts to taxonomy terms.
     *
     * Class Walker is defined in /wp-includes/class-wp-walker.php
     * Class Walker_Category is defined in /wp-includes/category-template.php
     */
    class MNA_Pad_Counts_Walker extends Walker_Category {
        /**
         * MLA Terms
         *
         * @var array
         */
        public $mla_terms = array();
    
        /**
         * Constructor - set the MLA Terms.
         *
         * @param string Taxonomy name/slug.
         */
        function __construct( $taxonomy ) {
            $attr = array (
                'taxonomy' => $taxonomy,
                'pad_counts' => 'true',
            );
            $terms = MLAShortcodes::mla_get_terms( $attr );
            unset( $terms['found_rows'] );
    
            foreach ( $terms as $term ) {
                $this->mla_terms[ $term->term_taxonomy_id ] = $term->count;
            }
        }
    
        /**
         * Start the element output.
         *
         * @see Walker::start_el()
         *
         * @param string Passed by reference. Used to append additional content.
         * @param object Taxonomy data object.
         * @param int    Depth of category in reference to parents. Default 0.
         * @param array  An array of arguments. @see wp_list_categories()
         * @param int    ID of the current category.
         */
        function start_el( &$output, $taxonomy_object, $depth = 0, $args = array(), $id = 0 ) {
    
            if ( isset( $this->mla_terms[ $taxonomy_object->term_taxonomy_id ] ) ) {
                $taxonomy_object->count = $this->mla_terms[ $taxonomy_object->term_taxonomy_id ];
            }
    
            parent::start_el( $output, $taxonomy_object, $depth, $args, $id );
        }
    }// Class MNA_Pad_Counts_Walker

    Then, create a walker object and add it to your wp_list_categories argument list:

    foreach ($taxonomies as $taxonomy) {
        unset( $checklist_walker );
        $checklist_walker = new MNA_Pad_Counts_Walker( $taxonomy );
        $args = array(
            'taxonomy'      => $taxonomy,
            'hierarchical'  => 1,
            'hide_empty'    => 0,
            'pad_counts'    => 1,
            'show_count'    => 1,
            'title_li'      => '',
            'walker'        => $checklist_walker,
        );
    
        echo '<li id="c' . $taxonomy . '" class="tax_list">' . get_taxonomy( $taxonomy )->label . '<ul>';
    
        wp_list_categories( $args );
    
        echo '</ul></li>';
    }

    That should get your the results you seek. Let me know if you have any problems with this technique or further questions.

    Thread Starter tosca30

    (@tosca30)

    Fantastic! This works perfectly.
    Thanks a lot for all the good work and the time spent on this question.

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘How to display pad_counts for hierarchical taxonomies’ is closed to new replies.