1

I want to get all post IDs from the current query. I know how to get all IDs of the current page using the following:

global $wp_query;
$post_ids = wp_list_pluck( $wp_query->posts, "ID" ); 

This will give me an array of all post IDs, but limited to the current page.

How can I get all IDs but not limited by 'posts_per_page'. (I don't want to modify the query by changing 'posts_per_page'.)

I know that there is already information available from the global $wp_query such as:

We will be displaying " . $wp_query->query_vars['posts_per_page'] . " posts per page if possible.

We need a total of " . $wp_query->max_num_pages . " pages to display the results.

Additional Details:

I am trying to get WooCommerce product IDs and hooking into the woocommerce_archive_description action to do this.

  • What exactly are you trying to do? I'm not sure what you'd need the IDs of all the posts to do. Your example is regarding the total number of posts/products per page, which you wouldn't need the IDs for. – Jacob Peattie Dec 18 '18 at 10:10
  • My end goal is to build a taxonomy filter. I want to use the post IDs from the main query to do a taxonomy look up. The post IDs array I get will be used in the args for a get_terms() request. – Gary Swift Dec 18 '18 at 10:23
  • WooCommerce that sort of filtering built in. Use attributes on your products and you can filter with those. – Jacob Peattie Dec 18 '18 at 11:11
  • I know how to filter the products. I am by hooking into ‘woocommerce_product_query’ (hookr.io/actions/woocommerce_product_query) and modifying the query. This is not what I am trying to do here at this stage. I want to use the post IDs of the current query to get all terms (product_tag) related to the current query. The get_terms function accepts an array of IDs to do this ($args["object_ids”]). I’m just wondering if there is a way to get all IDs of the current query. – Gary Swift Dec 18 '18 at 11:46
  • The current query is the same as the current page, you need a new query with different paged / posts_per_page values – Milo Dec 18 '18 at 14:07
0

Slightly old post I know but I just hit this exact issue myself.

Given a main_query find all the post IDs for it not limited by pagination.

I have made this function to return all terms for the main wp_query.

/*
 * Get terms for a given wp_query no paging
 * */
function get_terms_for_current_posts($tax='post_tag',$the_wp_query=false){
    global $wp_query;
    // Use global WP_Query but option to override
    $q = $wp_query;
    if($the_wp_query){
        $q = $the_wp_query;
    }

    $q->query_vars['posts_per_page'] = 200;// setting -1 does not seem to work here?
    $q->query_vars['fields'] = 'ids';// I only want the post IDs
    $the_query = new WP_Query($q->query_vars);// get the posts
    // $the_query->posts is an array of all found post IDs
    // get all terms for the given array of post IDs
    $y = wp_get_object_terms( $the_query->posts, $tax );
    return $y;// array of term objects
}

Hope this helps you or someone else stumbling across this.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.