Skip to content

The filter filter_count_user_posts() does not return the post count of custom post-types #900

Open
@IIM-Arvid

Description

@IIM-Arvid

This is possibly related to #231 and #494

Tested with:

  • Wordpress version: 6.0.2
  • CAP Plugin version: 3.5.6

The theme we are using uses the WP core function count_user_posts() for displaying the total post count for authors. When enabling CAP only the count for post-type "post" is returned. The count for all other custom post-types is not returned although we add an array with all post-types to be considered as the second argument to count_user_posts().

The function that is responsible for this behavior is filter_count_user_posts() in the class CoAuthors_Plus which gets hooked up on the filter get_usernumposts. And this filter is also applied in count_user_posts().

add_filter( 'get_usernumposts', array( $this, 'filter_count_user_posts' ), 10, 2 );

When commenting out this line, the right count is returned. So this function has to be responsible for it.

function filter_count_user_posts( $count, $user_id ) {
	$user     = get_userdata( $user_id );
	$coauthor = $this->get_coauthor_by( 'user_nicename', $user->user_nicename );

	// Return $count, if no coauthor exists.
	if ( ! is_object( $coauthor ) ) {
		return $count;
	}

	$term = $this->get_author_term( $coauthor );

	if ( is_object( $term ) ) {
		// Return combined post count, if account is linked.
		if ( strlen( $coauthor->linked_account ) > 2 ) {
			return $count + $term->count;
		}

		// Otherwise, return the term count.
		return $term->count;
	}

	// Return $count as fallback.
	return $count;
}

In this function the post count is fetched with

$term = $this->get_author_term( $coauthor );

which ultimately uses the WP Core function get_term_by()

	// See if the prefixed term is available, otherwise default to just the nicename
	$term = get_term_by( 'slug', 'cap-' . $coauthor->user_nicename, $this->coauthor_taxonomy );
	if ( ! $term ) {
		$term = get_term_by( 'slug', $coauthor->user_nicename, $this->coauthor_taxonomy );
	}

I looked up the author slug (cap-' . $coauthor->user_nicename) in the table wp_terms to get the term_id for it. After that I searched for that term_id in the table wp_term_taxonomy. And in this table there is a column "count" which is apparently read. But this column contains only the number of posts of post-type "post". The count for all other custom post-types is not included there.

But if you can see in your function filter_count_user_posts() the right $count is already passed as an argument. And this $count even is returned on several lines except here:

	if ( is_object( $term ) ) {
		// Return combined post count, if account is linked.
		if ( strlen( $coauthor->linked_account ) > 2 ) {
			return $count + $term->count;
		}

		// Otherwise, return the term count.
		return $term->count;
	}

if the account is not linked it only does return $term->count; which outputs the value of the count column from wp_term_taxonomy. Therefore your function always returns only the count for posts of post-type "post". But even if the account would be linked the count would be wrong because it does not consider custom post types. And since it´s always hooked up on that filter it´s wrong everywhere on the website. If you ask me the problem lies somewhere in this part of the function. The function should always at least output $count. Or maybe it has to be rebuild from the ground up to support custom post-types.

Unfortunately I couldn't even use remove_filter() to remove your function because I would have to use an instance of the CoAuthors_Plus Class as the callback argument for it. And doing so throws an error. So my quick and dirty fix is

unset( $wp_filter['get_usernumposts'] );

which is very bad of course ;) So it would be nice, if you could address this problem.

Steps to reproduce:

  1. Use Wordpress 6 with CAP Plugin 3.5.6
  2. register a custom post type
  3. write a post with a specific author
  4. write another post with the same author but for the custom post-type you registered before
  5. call the wp core function count_user_posts() somewhere in a frontend template. Add an Array with both post-types as the second argument.
  6. the output only shows 1 but it should output 2 since you added an array with both post-types in step 5.
  7. If you comment out the line where you add your function to the filter (line 99) it now outputs the right count of 2

Thank you very much for your help

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions