0

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

Your Answer

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

Browse other questions tagged or ask your own question.