• Resolved Sany

    (@sannny)


    Hello,

    the count columns in a taxonomy list table is counting only published posts with a term. Custom post status aren’t considered.
    I want all posts to be counted with any post status (maybe no trashed posts), also draft, pending…
    How can I achieve this?
    The name of the column is posts.

    I tried with filters add_filter('manage_edit-custom-taxonomy_columns', 'add_columns'); and add_filter('manage_custom-taxonomy_custom_column', 'add_columns_content', 10, 3); but without success.

    Best regards
    saNNNy

Viewing 6 replies - 1 through 6 (of 6 total)
  • You can Use Wp_Query to get posts count in each term.
    More details here:
    https://wordpress.stackexchange.com/a/121768

    Thread Starter Sany

    (@sannny)

    Thanks for your answer. I will try this later.

    But the primary question is: how can I override the values in the column posts? Do I need to delete the column and add a new one or can I override it? I just try to echo “hello” but it doesn’t appear.

    • This reply was modified 7 years, 10 months ago by Sany.
    • This reply was modified 7 years, 10 months ago by Sany.
    Moderator bcworkz

    (@bcworkz)

    Altering the values in the existing column would be a bad idea. I’m not sure how WP makes use of these values, but altering them could lead to strange behavior. Perhaps improper pagination for term queries for example. Altering default table structures is also a bad idea. Doing so can prevent WP form properly updating the DB when needed. It could be a long while before there’s a problem, but sooner or later it will be a problem.

    If such revised data is really needed to be kept in a separate column, you could add your own table and join it to the WP tables if necessary. It might suffice to store your values in term meta. Depending on your need, it may suffice to make a count query each time you need a value, as Aboelabbas is suggesting, and not store anything.

    The extra column would only be necessary if you are making very large, complex queries where including data from term meta is simply taking too long, you need greater efficiency. In most cases, using data in term meta should be adequate for queries. If the data is needed only for informational output, getting a count on the fly would likely be enough, no need to store the values at all.

    Thread Starter Sany

    (@sannny)

    I agree. Altering the values in the existing column was a bad idea. So I decided to add a new one. WP offers hooks/filters for adding custom columns so I don’t know how this depends with the database, it’s just an output.

    The following solution is working fine for me:

    I’m using
    add_filter('manage_edit-details_columns', 'add_count_all_column');
    for adding a new column and add_filter('manage_details_custom_column', 'add_countall_column_content', 10, 3);
    for outputting the content.
    I’m counting the posts with the following query:

    
    $args = array(
      'post_type' => 'product',
      'tax_query' => array(
        array(
          'taxonomy' => 'details',
          'field' => 'id',
          'terms' => $term_id
        )
      ),
    );
    $the_query = new WP_Query($args);
    $count = $the_query->found_posts;
    echo $count;

    Thanks for your help guys. I can close this topic now.

    • This reply was modified 7 years, 10 months ago by Sany.
    • This reply was modified 7 years, 10 months ago by Sany.
    • This reply was modified 7 years, 10 months ago by Sany.
    Moderator bcworkz

    (@bcworkz)

    Great news!

    FWIW, I thought when you mentioned adding columns, you meant adding to the default wp_posts DB table. That is a problem. Adding columns to the posts list table is perfectly fine, that’s why the hooks exist ??

    Thread Starter Sany

    (@sannny)

    ok cool ??

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Term Count Column counts only published posts’ is closed to new replies.