wp query - Assign Json file to WP_Query

admin2025-06-02  2

I am working with an unnecessarily complex theme with very limited time. Because of the number of records we have, load time is unbearably slow therefore we are looking to caching records using json files (would rather not use transients and avoid the db altogether).

The theme uses WP_Query ($args) to fetch results from the DB. We would like to fetch the query one time only and use a json cached file for subsequent requests.

The issue is because of the number of files we have, we would have to modify and dive even deeper into the inner working of the theme to use the json file.

So instead of this:

$my_query1 = new WP_Query($args);
                $wp_query = null;
                $wp_query = $my_query1;

this:

if (!is_file($file)) {
                $my_query1 = null;
                $my_query1 = new WP_Query($args);
                $wp_query = null;
                $wp_query = $my_query1;
                $contents = json_encode($wp_query);
                $fp = fopen($cachepath.'.json', 'w');
                fwrite($fp, $contents);
                fclose($fp);}

if (is_file($file)){
$str = file_get_contents($file);
$my_query1 = json_decode($str, true);
$wp_query = $my_query1;
}

The above works to create the Json file but because of the numerous hooks and actions we would like to reuse the WordPress functions to loop through posts like this.

<?php if( $my_query1->have_posts() ) : 
                while ($my_query1->have_posts()) : $my_query1->the_post(); ?>   

is there a way to assign the json files to WPQuery class so the above will still function as expected?

I am working with an unnecessarily complex theme with very limited time. Because of the number of records we have, load time is unbearably slow therefore we are looking to caching records using json files (would rather not use transients and avoid the db altogether).

The theme uses WP_Query ($args) to fetch results from the DB. We would like to fetch the query one time only and use a json cached file for subsequent requests.

The issue is because of the number of files we have, we would have to modify and dive even deeper into the inner working of the theme to use the json file.

So instead of this:

$my_query1 = new WP_Query($args);
                $wp_query = null;
                $wp_query = $my_query1;

this:

if (!is_file($file)) {
                $my_query1 = null;
                $my_query1 = new WP_Query($args);
                $wp_query = null;
                $wp_query = $my_query1;
                $contents = json_encode($wp_query);
                $fp = fopen($cachepath.'.json', 'w');
                fwrite($fp, $contents);
                fclose($fp);}

if (is_file($file)){
$str = file_get_contents($file);
$my_query1 = json_decode($str, true);
$wp_query = $my_query1;
}

The above works to create the Json file but because of the numerous hooks and actions we would like to reuse the WordPress functions to loop through posts like this.

<?php if( $my_query1->have_posts() ) : 
                while ($my_query1->have_posts()) : $my_query1->the_post(); ?>   

is there a way to assign the json files to WPQuery class so the above will still function as expected?

Share Improve this question asked Mar 11, 2019 at 21:09 DarioDario 111 bronze badge 1
  • 2 Keep in mind that if your queries are slow, that's no guarantee that the filesystem will be faster. Your site can handle millions of posts if the queries are written right, but some data decisions ( such as searching and filtering posts via data stored in their post meta, or using NOT IN style queries ) can make large quantities of posts, or even lower numbers below 500 slow to a crawl, I don't believe the JSON files are going to do what you hope they'll do – Tom J Nowell Commented Mar 11, 2019 at 21:39
Add a comment  | 

1 Answer 1

Reset to default 0

Found the solution. $wp_query returns an array of objects. Therefore when decoding the json set teh second parameter to false like so

 json_decode($str, false);

Then assign the returned values to object vars like so:

if (is_file($file)){
    $str = file_get_contents($file);    
    $my_query_raw = json_decode($str, false);
    $my_query1 = null;
    $wp_query = null;
    $my_query1 = new WP_Query;
    $posts_son = ($my_query_raw->posts);

    $my_query1->query = $my_query_raw->query;
    $my_query1->posts = $posts_son;
    $my_query1->request = $my_query_raw->posts;
    $my_query1->post_count = count($my_query_raw->posts);

    $found_posts = $my_query_raw->found_posts;
    $max_num_pages = $my_query_raw->max_num_pages;

    if ( isset($found_posts)) $my_query1->found_posts = $my_query_raw->$found_posts;
    if ( isset($max_num_pages)) $my_query1->max_num_pages = $my_query_raw->$max_num_pages;

//assign to $wp Query
$wp_query = $my_query1;
        }
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748810749a313915.html

最新回复(0)