0

I have 2 functions that use the same $args for the WP_Query class. How can i make it so I have to define the WP_Query class only once, and share all the returns, i.e. I should be able to loop through the query in the two functions without having to define it in each one.

Example:

function first_function(){
  $args = array('post_type' => 'accomodation', 'posts_per_page' => 4, 'meta_query' => array('key' => 'accomodation_type', 'value' => get_query_var('accomodation_type')));
  $query = new WP_Query($args);

  while ($query->have_posts()){
   $query->the_post();

   echo '<h1>' get_the_title() '</h1>';
  }
}
add_action('load_first_function', 'first_function');

function second_function(){
  $args = array('post_type' => 'accomodation', 'posts_per_page' => 4, 'meta_query' => array('key' => 'accomodation_type', 'value' => get_query_var('accomodation_type'));
  $query = new WP_Query($args);

  while ($query->have_posts()){
   $query->the_post();

   echo '<h1>' get_the_title() '</h1>';
  }
}
add_action('load_second_function', 'second_function');
4
  • Is the only difference the number of posts? – Tony Djukic Mar 15 at 18:06
  • @TonyDjukic Hmm. I think there won't be any differences in the $args, I will update the question. I only would like to have to define it once, because i have multiple meta_query values and i don't want to have to define it each time. I hope it makes sense. – Enthusiast Mar 15 at 18:19
  • If the number of posts per page is different then they are not the same. Note that you should not expect to see performance gains, WordPress holds a copy of posts in memory so that it doesn't request the same post multiple times, you would not be saving any time or resources, or avoiding database queries – Tom J Nowell Mar 15 at 18:21
  • @TomJNowell I have updated my question, the number of posts can be the same. Its really not about performance, i just want to keep my code DRY. I have multiple meta_query values, that i am getting using get_query_var function, i don't want to define it each time. Note that each function has its own use case, i dont want to write all the details cause it can get confusing. – Enthusiast Mar 15 at 18:25
2

If you want to reduce code duplication, then extract the code into its own function, e.g.:

function get_xyz_query_args( ) : array {
    return array('post_type' => 'accomodation', 'posts_per_page' => 4, 'meta_query' => array('key' => 'accomodation_type', 'value' => get_query_var('accomodation_type')));
}

....

$args = get_xyz_query_args();
$q = new WP_Query( $args );

However, there is very little performance to be gained by recycling the WP_Query object, and potential losses if it's done correctly. The complexity introduced would mean new bugs.

Since WP stores posts it fetches and post meta in WP_Cache, the second WP_Query only needs to fetch post IDs. On top of that, any caching plugin or object cache present can eliminate the database query completely.

So this is not worth doing if performance is your goal, there are better ways.

1
  • 1
    This is actually a good solution and works in my case, I somehow didn't think of it. Thanks! – Enthusiast Mar 15 at 18:35

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.