• Quick question!

    I’m trying to figure out a simple way to let the built-in category count to also include pending posts, but haven’t been able to find a good way to do so.

    The reason I need this is that I use an external tool that uses the WP-JSON API to push posts into WordPress and includes the post count +1 in the title. However, if the last post is still pending, it uses the same number again. So if there are 5 published posts and 1 pending, the new post will still get “6” pushed into it, not “7” like it should.

    Any thoughts?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Are you able to affect the coding in the external tool? If so, you could ask the author to adjust the query they are using to retrieve the list of posts by category, and include “pending” and “draft” posts to that query. This way they will get an accurate number to append when they send you a title.

    It looks like you can filter by status in the WP-API when fetching posts: https://v2.wp-api.org/reference/posts/

    Thread Starter Qliphoth

    (@qliphoth)

    Sorry, no can do. The count field correlates with a value stored in the database, so grabbing information about a taxonomy will always only show the amount of posts that are published in that category, no matter what arguments you apply.

    The external tool is one of my own making as well, so I’m in 100% control of the code, but no matter how I turn this around, it all comes back to the same thing, I have to change how WordPress returns the “count” of the taxonomy. It has to include all posts in a taxonomy, not just published posts.

    Hmm. What are you using to get the count through the API? Just the term retrieval?

    What I suggested earlier would have you do your own “count” calculation, by changing WP_Query parameters (which is not the most efficient way to do what you’re trying to do, upon reflection).

    But, since you are building both ends of the tool, you can do your own calculation about how many posts have a particular category applied to them. Which means you can create your own API call to do this, rather than changing how WordPress does it.

    The post count for each term (category, tag, etc) is stored in the database (in the wp_terms_taxonomy table), and it gets updated when a post is published and updated. Then, when the term information is read, the count is read as well.

    If you want to see about changing how WordPress does it, take a look at wp-includes/taxonomy.php and wp-includes/post.php. In there you can read more about the various hooks that fire on saving posts, updating posts, and term/object associations. There may be a series of hooks that you can use to add to the count when a post is saved, regardless of post status.

    I don’t have time right now to read through these files, but I hope this is helpful. And maybe someone who’s actually done this can chip in.

    Thread Starter Qliphoth

    (@qliphoth)

    Right, I’ve been looking at that, and the problem is how _update_post_term_count() for taxonomies attached to post types first confirms that the objects are published before counting them.

    Ideally, I would like for the default way WordPress works (by hooking into it via functions.php or a plugin) would be for WordPress to just count all posts in a category against it, no matter if the post is in draft, pending, scheduled or published. It feels like that would be less work and require less administration (and fewer possible scenarios that could cause errors) than creating a separate API call to fetch only this “special” count, when I’m already fetching the other category data (id, name, slug, etc) using the normal API call.

    It all boils down to this one row in taxonomy.php:

    $count += (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_relationships, $wpdb->posts WHERE $wpdb->posts.ID = $wpdb->term_relationships.object_id AND post_status = 'publish' AND post_type IN ('" . implode("', '", $object_types ) . "') AND term_taxonomy_id = %d", $term ) );

    I’m not going to change any of the core WordPress files, I just want the function to ignore that “post_status” part of the query by hooking into it from elsewhere.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Post Count to include Pending’ is closed to new replies.