• yjarid

    (@yjarid)


    i am trying to get the number of posts (for a custom post type) by author, first I used count_user_posts($userID , 'recipe') then i trried the work around :

    function nbrPostByUser( $userid, $post_type = ['recipe'] ) {
            global $wpdb;
        
            $where = get_posts_by_author_sql( $post_type, true, $userid );
        
            $count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->posts $where" );
        
              return apply_filters( 'get_usernumposts', $count, $userid );
        }

    it always returns 0 …. after debugging I think that the cause is that my custom post type is not included in the global $wp_post_types.

    are my approaches correct ? if yes what went wrong then?

    Thanks

    • This topic was modified 6 years ago by bcworkz. Reason: code fixed
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Instead of taking a bunch of time to debug where this goes wrong, why not compose a complete SQL query that gets the count you need without relying upon anything but the global $wpdb?

    Why apply filters? It just gives something else a chance to mess up the answer you want.

Viewing 1 replies (of 1 total)
  • The topic ‘Post count for custom post retutns 0’ is closed to new replies.