• I have a custom post type called “Products” which I am then calling

    count_user_posts(425, 'product')

    This SHOULD return 624, but it returns 0. I went into

    wp-includes/user.php:554 and added some logging to see what was going on.

    function count_user_posts( $userid, $post_type = 'post', $public_only = false ) {
    	global $wpdb;
    
    	$where = get_posts_by_author_sql( $post_type, true, $userid, $public_only );
    	$count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" );
    	
    	if($userid === 425){
    		var_dump($count);
    	}
    
    	/**
    	 * Filters the number of posts a user has written.
    	 *
    	 * @since 2.7.0
    	 * @since 4.1.0 Added <code>$post_type</code> argument.
    	 * @since 4.3.1 Added <code>$public_only</code> argument.
    	 *
    	 * @param int          $count       The user's post count.
    	 * @param int          $userid      User ID.
    	 * @param string|array $post_type   Single post type or array of post types to count the number of posts for.
    	 * @param bool         $public_only Whether to limit counted posts to public posts.
    	 */
    
    	if($userid === 425){
    		var_dump(apply_filters( 'get_usernumposts', $count, $userid, $post_type, $public_only ));
    	}
    
    	return apply_filters( 'get_usernumposts', $count, $userid, $post_type, $public_only );
    }

    The first var_dump returned 624 and then second var_dump returned 0. So I then searched my project and found

    co-authors-plus/co-authors-plus.php:95 was using the get_usernumposts filter. I simply commented that line out and then both of my var_dump tests returned the correct count.

    I am pretty sure it has to do with $post_type not being accounted for but I am not sure how to get around this. Any suggestions?

Viewing 1 replies (of 1 total)
  • Thread Starter Matt Pramschufer

    (@mattpramschufer)

    As a work around, i simply created a new function to get the post count of the user,

    	public function get_product_count_by_user_id($user_id){
    		global $wpdb;
    		$where = get_posts_by_author_sql( 'product', true, $user_id, true );
    		return $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" );
    	}

    however that will not take into account if a coauthor is the author of a post. So would still like to see if I can get this working with using standard wordpress function instead of custom function.

Viewing 1 replies (of 1 total)
  • The topic ‘Compatibility issue with get_usernumposts filter’ is closed to new replies.