In one of my template files, I am running the standard WP_Query and loop etc. This is not within the product archive etc, just a regular custom template.

<?php $args = array(
    'post_type' => 'product',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'orderby' => 'title',
    'order' => 'ASC',
    'meta_query' => $meta_query
); ?>

<?php $post_query = new WP_Query( $args ); ?>
        <?php if ( $post_query->have_posts() ) : ?>


From my filters.php file, I want to hook into that WP_Query and grab the IDs returned. I suspect it's a pre_get_posts approach but I need it to be focusing on the WP_Query which I am running to get products. Elsewhere on the site are other WP_Query's being ran.

How can I hook into the currently running WP_Query for product post type and grab the IDs. I don't want to do this within the loop in the template file.

  • What do you want to do with the IDs? With this kind of query you'd only be able to re-use the IDs after the query is used in the template. Jul 16, 2023 at 8:54

1 Answer 1


The most efficient approach: Just run the query again wherever you need it.

Add this to the $args array: 'fields' => 'ids',

$post_query->posts will only be an array of product ids.

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.