WordPress.org

Ready to get started?Download WordPress

Forums

Fetch all post ids of a certain post_type (3 posts)

  1. Mario
    Member
    Posted 10 months ago #

    Hi,
    i need to extract just the IDs of the posts of a set post_type.
    I currently use :
    query_posts("post_type=mytype&posts_per_page=-1") and i iterate through the results to get the IDs...but i need a faster way to directly query the IDs without the overhead of fetching everthing of a post.
    What's a better way to do that? Thank you

  2. David Choi
    Member
    Posted 10 months ago #

    Try

    $custom_query = new WP_Query(array(
            'post_status' => 'publish', /* published ones only */
            'post_type' => 'post,page', /* in this case including 'post' and 'page' types */
            'order' => 'ASC',
            'order_by' => 'ID'
        ));

    http://codex.wordpress.org/Class_Reference/WP_Query

  3. Ben Huson
    Member
    Posted 10 months ago #

    Use the WP_Query as above but with the 'fields' parameter to just return IDs.

    Or if you are returning post objects you can use wp_list_pluck() to get an array of the IDs rather than iterating through all of them.

Reply

You must log in to post.

About this Topic

Tags

No tags yet.