Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic 🚀

    The Stats interface doesn’t allow you to sort by post type, but you can use the WordPress.com Stats API to build your own reports, and get stats for all posts belonging to a specific post type.

    You can learn more about the API here:
    http://stats.wordpress.com/csv.php
    https://phoxis.org/2011/04/24/wordpress-com-stats-api/

    Thread Starter b-rad

    (@b-rad)

    Hi Jeremy,

    I visited the links you posted and unless I’m missing it I don’t see a parameter that allows me to specify a custom post type. I see a way to request stats for a specific post, but not all posts in a custom post type.

    Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic 🚀

    That’s correct. However, since you’re building your own query, you could get a list of post IDs belonging to a specific post type, and then get starts for those post IDs.

    Thread Starter b-rad

    (@b-rad)

    Dang. Unfortunately that’s not feasible as others are creating content under that post type and having to manually build a list of posts would be unsustainable. Looks like I’ll need to find another way to get those stats. 🙁

    Plugin Author Jeremy Herve

    (@jeherve)

    Jetpack Mechanic 🚀

    You don’t have to build that list manually though. You could build a custom query to return all posts in that post type, like so:

    /**
     * Query Arguments.
     * In this example, I look for Jetpack Portfolio posts.
     * I use the <code>fields</code> parameter to only return post IDs, because we don't need anything else here.
     */
    $args = array(
    	'post_type'      => 'jetpack-portfolio',
    	'fields'         => 'ids',
    	'posts_per_page' => '-1'
    );
    
    $custom_query = new WP_Query( $args );
    
    // $custom_query->posts will then return an array of all the post IDs in that post type.
    var_dump( $custom_query->posts );

    I should mention, however, that querying for all the posts can be a slow query if you have a lot of posts in that post type.

    I’d consequently recommend the following:

    • Only look at a portion of the posts if that can be done (the last 100, for example).
    • Cache the results.
    • Cache the results you get from the WordPress.com Stats API as well.
    Thread Starter b-rad

    (@b-rad)

    Hmmm, I didn’t think of building a plugin for this, but that would probably work. Little more work than I was hoping for, but as a fallback I could see this working. Thanks for the suggestion.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Get stats for specific custom post type?’ is closed to new replies.