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.