2

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.

5
  • 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. Commented Dec 18, 2018 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
    Commented Dec 18, 2018 at 10:23
  • WooCommerce that sort of filtering built in. Use attributes on your products and you can filter with those. Commented Dec 18, 2018 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
    Commented Dec 18, 2018 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
    Commented Dec 18, 2018 at 14:07

2 Answers 2

2

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.

0

Wanted to share my two cents.

Indeed, the main $wp_query will only return the posts correlating to the current Pagination Parameters.

Like Milo pointed out in the comments you can actually create a new query with a different set of pagination parameters (eg: nopaging, posts_per_page, posts_per_archive_page ...).

We can mirror the main query parameters and merge them with our new pagination parameters. As we're only interested in the posts ids, we can use the fields query parameter to only return the posts ids.

/**
 * Retrieve the IDs of the items in the main WordPress query.
 * 
 * Intercept the main query, retrieve the current parameters to create a new query.
 * Return the IDs of the items in the main query as an array.
 * 
 * @see https://wordpress.stackexchange.com/a/400947/190376
 * 
 * @return Array The IDs of the items in the WordPress Loop. False if $post is not set.
 * 
 * @since 1.0.0
 */
public function wpso_323280() {

    if ( is_main_query() ) {

        global $wp_query;

        $buffer_query = $wp_query->query;
    
        $args = array(
            'nopaging' => true,
            'fields' => 'ids',
        );
    
        $custom_query = new WP_Query( array_merge( $buffer_query, $args ) );
    
        wp_reset_postdata();
    
        return $custom_query->posts;

    };

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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