0

I have finished building my website and noticed due to the structure it takes 3 seconds to load the data due to each WP_query bouncing between different post types. I was wondering if I put these into one function run so I do not have to run a array each time, if this would have any beneficial effect?

Sadly at moment i have tried:

function get_gametitle_info() {

global $post;

$game_terms = get_the_terms($post, 'games_titles');
$game_con = wp_list_pluck( $game_terms, 'slug' );

$game_info = array(
    'post_type'=> 'game_information',
    'posts_per_page' => 1,
    'post_status' => 'publish',
    'order'    => 'ASC',
    'tax_query' => array(
        array(
        'taxonomy' => 'games_titles',
        'field' => 'slug', 
        'terms' => $game_con,
        ), 
    )
);      

$game_info_query = new WP_Query( $game_info );
while ( $game_info_query->have_posts() ) : $game_info_query->the_post();

$gametitleID = get_the_ID();  
    if (has_term('3pegi', 'age_ratings', $post)){
    $age_terms = array('3pegi'); } 
    if (has_term('7pegi', 'age_ratings', $post)){
    $age_terms = array('3pegi', '7pegi'); } 
    if (has_term('12pegi', 'age_ratings', $post)){
    $age_terms = array(array('3pegi', '7pegi', '12pegi')); }
    if (has_term('16pegi', 'age_ratings', $post)){
    $age_terms = array('3pegi', '7pegi', '12pegi', '16pegi'); }
    if (has_term('18pegi', 'age_ratings', $post)){
    $age_terms = array('3pegi', '7pegi', '12pegi', '16pegi', '18pegi'); }
endwhile;

return $gametitleID;
return $age_terms;
return $game_info_query;
}

This gives me a - Warning: Invalid argument supplied for foreach() in D:\xampp\htdocs\wp-includes\functions.php on line 2721

The game_info array only needs to be ran once, i would then love it to be able to use that data instead of doing a new run each times on all other matching querys when i switch the post type back after using a wp_reset_postdata();

Sorry if it is not clear. Another way to explain is I have:

[post [A post] post post [A post] post [B post] [A post] ]

The constant switching seems abit pointless to me when in truth I only need to do the wp_query once for A and B. Would this save on resources and reduce the 3 second load? Thanks

Normal structure inside post file:

$currentID = get_the_ID();
$game_terms = get_the_terms($post->ID, 'games_titles');
$game_con = wp_list_pluck( $game_terms, 'slug' );
$args = array(
    'post_type'=> 'game_information',
    'posts_per_page' => 1,
    'post_status' => 'publish',
    'order'    => 'ASC',
    'tax_query' => array(
        array(
        'taxonomy' => 'games_titles',
        'field' => 'slug', 
        'terms' => $game_con,
        ), 
    )/*Search above post type for taxonomy and if matches then proceed*/
);             
$the_query = new WP_Query( $args );
if($the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post();
$gametitleID = get_the_ID();  
    if (has_term('3pegi', 'age_ratings', $post->ID)){
    $age_terms = array('3pegi'); } 
    if (has_term('7pegi', 'age_ratings', $post->ID)){
    $age_terms = array('3pegi', '7pegi'); } 
    if (has_term('12pegi', 'age_ratings', $post->ID)){
    $age_terms = array(array('3pegi', '7pegi', '12pegi')); }
    if (has_term('16pegi', 'age_ratings', $post->ID)){
    $age_terms = array('3pegi', '7pegi', '12pegi', '16pegi'); }
    if (has_term('18pegi', 'age_ratings', $post->ID)){
    $age_terms = array('3pegi', '7pegi', '12pegi', '16pegi', '18pegi'); }
    endwhile; else:  endif;  wp_reset_postdata();

To further clarifiy I am looking for a way to call the wp_query once on page load (specific to that page) and save the data as a variable to be used within the header, main, sidebar and footer. But NEEDS to be refreshed for each unique page load as the data is highly dynamic and needs to be the latest. At the moment i call multiple wp_query then reset the postdata each time (even if the wp_query calls the same data) due to the way I am required to switch between post types.

Simplest way I can possibly manage to explain - Looking for a way to optimize multiple wp_query's of the same data into one call, but able to be used at multiple locations within a template.

  • 1. performing exactly the same query multiple times on a single page load just loads the data from the object cache on subsequent queries, so you're only querying the db once 2. WordPress template files are loaded via functions, so to access variables between template files, they must be globalized. – Milo Dec 15 '13 at 16:15
  • Brilliant. Would explain alot of tests I have been running. Been using the same Query name on post switching so by giving them unique query names would separate the caches? Will give this a test now. – Serpream Dec 15 '13 at 16:22
  • it doesn't matter what they're named, WordPress hashes the query parameters to create a unique string for each query, so it'll pull from cache for any query that generates an existing hash. the object cache isn't persistent, so this only works within a single page load. – Milo Dec 15 '13 at 16:25
  • @Milo Yeh your advice pointed me towards my glaring flaw. I kept creating a new query and refreshing the cache, rather than using what was present. The Global element also allows me to solve other issues. If you put it as the answer I can then tick it. Not enough rep to write my own which seems daft. Thank You for help + advice. – Serpream Dec 15 '13 at 16:41
0

Your description of the problem is not very clear, and your inconsistent code formatting makes that hard to read (plus I am pretty sure some code is missing), but if the problem is running the query multiple times you can solve that with a static variable.

function get_gametitle_info() {
  static $game_info_query = false;
  if (false !== $game_info_query) {
    return $game_info_query;
  }
  /*
    the rest of your code
  */
}

You are sort-of "caching" the result, good for the single page load which should help. You can "cache" over multiple page loads using the Transient API.

function get_gametitle_info() {
  $game_info_query = get_transient( 'game_info_query' );
  if ( !empty( $game_info_query ) ){
    return $game_info_query;
  } 
  /*
    (most of) the rest of your code
  */
  set_transient('game_info_query', $game_info_query, HOUR_IN_SECONDS );
  return $game_info_query;
}

That should save the data to the database for an hour before updating it. Obviously you can choose some other time frame. The single simple query should be quicker than the complicated query.

You could even combine the two approaches, though I haven't tested and I am not sure of the effectiveness of doing so.

function get_gametitle_info() {
  static $game_info_query = false;
  if (false !== $game_info_query) {
    return $game_info_query;
  }

  $game_info_query = get_transient( 'game_info_query' );
  if ( !empty( $game_info_query ) ){
    return $game_info_query;
  } 

  /*
    (most of) the rest of your code
  */
  $game_info_query = 'howdy';
  set_transient('game_info_query', $game_info_query, HOUR_IN_SECONDS );
  return $game_info_query;
}
  • Sorry for being unclear. My website is heavily modified and at moment i have to call the query for header, main and sidebar as the wp_query does not seem to be shared over templates. Will try this code now and give feedback. The caching system sounds extremely interesting as my content is constantly changing based off multiple elements being pulled from various custom posts. So being saved for a hour or so would be stunning. Thanks for response and will get back asap once tested. – Serpream Dec 15 '13 at 15:40

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.