• WP does not include Privates Posts in calculating the “count” field of any particular Category record in Table: wp_term_taxonomy, even if there are Private Posts in that Category. I would like to override this so that Private Posts ARE include in the count. Where/How is the “count” field calculated? I’m comfortable modifying MySQL, but those changes are likely to be lost in an upgrade.

    So, alternatively, is there an action/function I can add in my Child-theme functions.php to force the display of Categories that are only associated with Private Posts?

Viewing 9 replies - 1 through 9 (of 9 total)
  • in your query you can add the post status as an array:

    
    'post_status' => array('publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash') 
    Thread Starter eliant

    (@eliant)

    I understand your code. But to what query are you referring? In which file/function?

    Thread Starter eliant

    (@eliant)

    Thanks for pointing me to WP_Query. Good information. However, that modifies the criteria for selecting Posts, and I need to modify the “count” field of the Category taxonomy records so that Private items are counted (which they are not in standard WP).

    I have a landing page for when employees log in. It is supposed to list all the Categories of articles they can review. However, Private articles (which is all of them) are not counted, so the list is empty.

    The landing page is a simple Title block and a List Categories block.

    Okay how are you getting this list of categories?

    for example:

    <ul>
    <?php
    $categories = get_categories();
    foreach ($categories as $cat) {
    	$query = '';
    	$args = array(
    		'cat' => $cat->term_id,
    		'post_status' => array( 'private','publish' ),
    	);
    
    	$query = new WP_Query( $args );
    	print_r($the_query);
    	echo "<li>".$cat->cat_name." (".$query->found_posts.")</li>";
    	wp_reset_query();
    }
    
    ?>
    </ul>
    Thread Starter eliant

    (@eliant)

    In WP Dashboard, I took these actions:
    – created new page (Private) for employees’ landing page,
    – using block editor, added title, then added the “Category” widget,
    – Published it.

    If I have categorized Posts that are NOT Private, the categories of those Posts show up on the list.
    If I have a category in which ALL the Posts are Private, that category does not show up on the list.

    That’s because the “count” field for that particular category in the wp_term_taxonomy table is set to zero. As I’ve learned in my internet searching, that is the norm for WP; it does not include Private posts when calculating the number of entries in a given category. And if the category count is zero, the Category Widget does not display the category.

    Thread Starter eliant

    (@eliant)

    I’m comfortable with MySQL and PHP, but this is my first venture into the inner-workings of WP. Can I modify the Categories Widget in such a way that my changes aren’t lost in a future update?

    I’ve also looked for a MySQL trigger event in phpMyAdmin, but I’m not seeing one. I don’t believe “count” in wp_term_taxonomy is an autocount field from another table. If anyone can tell me where the “count” field is set (via MySQL or a php function) I feel I can make a work-around.

    • This reply was modified 4 years, 2 months ago by eliant.

    well there is the filter: https://codex.www.ads-software.com/Plugin_API/Filter_Reference/widget_categories_args

    Or you could roll your own widget.

    Or you can try: https://www.ads-software.com/plugins/php-code-widget/

    then in the widget, add the code I pasted above and see if that provides the result you want.

    Thread Starter eliant

    (@eliant)

    Is it possible to contact the WP development team directly to ask where the wp_term_taxonomy “count” field is calculated and written to the database?

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Custom calculation of wp_term_taxonomy Category “count”’ is closed to new replies.