$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - Assign Json file to WP_Query|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp query - Assign Json file to WP_Query

matteradmin9PV0评论

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;
        }

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far