Akram ul haq Akram ul haq - 19 days ago 7
PHP Question

WordPress categories are taking huge time in page load

I am getting my categories with json encode and so my formatted data is json format, and that data is being used in mobile app, at the moment every thing is working fine except categories. Categories are taking 8 to 10 seconds to load.
here is my data I have written to achieve this task

public function category() {
global $post; global $cate_id;
if(isset($_GET['category_id'])){
$cat_id = $_GET['category_id'];
//$cat = get_category($cat_id);
//$cat_name = $cat->slug;
$args = array(
'post_type' => 'post',
'posts_per_page' => 10,
'category' => $cat_id
//'category_name' => 'cancer-care'
);
$posts = get_posts($args);
}
//print_r($posts);
if(!empty($posts)){
foreach ($posts as $post) : setup_postdata( $post );
$post_title = $post->post_title;
$post_content = $post->post_excerpt;
$post_fullcontent = apply_filters ("the_content", $post->post_content);//$post->post_content;
$post_link = get_the_permalink($post->ID);
$post_image = wp_get_attachment_url( get_post_thumbnail_id($post->ID) ,'thumbnail_size');
$post_image_thumb = get_bloginfo('template_url').'/thumbs/timthumb.php?src='.$post_image.'&w=438&h=220&zc=1&a=c&q=100';

$category_name = get_cat_name(get_post_meta($post->ID, 'home_page_category', true));
$category_id = get_post_meta($post->ID, 'home_page_category', true);
$category_color = categoryCustomFields_GetCategoryCustomField($category_id, 'Color Code');
$category_url = get_category_link(get_post_meta($post->ID, 'home_page_category', true));

//earlier done: echo $totalcount = $this->social_shares($post_link);

$result[] = array(
'post_id'=>$post->ID,
'post_title' => $post_title,
'post_short_content' => $post_content,
'post_full_content' => $post_fullcontent,
'post_link' => $post_link,
'post_image' => $post_image,
'post_image_thumb' => $post_image_thumb,
'category_name' => $category_name,
'category_color_code' => $category_color[0]->field_value ? $category_color[0]->field_value : '#83ab44',
'category_id' => $category_id,
'category_url' => $category_url,
'total_social_share' =>$this->social_shares($post_link),
'post_by' =>get_the_author(),
'post_date' =>date('Y-m-d', strtotime($post->post_date)),
'post_time' =>date('H:i:s', strtotime($post->post_date))
);
endforeach;
$message = array(
"success" => "true",
"error" => "null",
"post_data" => $result
);
echo json_encode(array('response' => $message));
}else{
$message = array(
"success" => "false",
"error" => "Record not available",
"post_data" => "Record not available"
);
echo json_encode(array('response' => $message));
}
}


As soon as go with all my other data like
home_psots
function to show home posts, or
feature_post
function to show featured posts etc are showing results abruptly ... instead categories ... as I am getting categories like this one
category?category_id=4 any idea so that I can get my data faster than this one ... ??? I have tried a lot of things, even played with
.htaccess
file to redirect to other functions which I wrote but in vain ...:(

Answer

That's the whole difference in your page load time. Other pages which are loading without URL parameter are being cached and takes less time in further loadings. While W3 Total Cache Plugin does not cache query based URis when Disk Enhanced page cache method is used. That's why your query based pages are not being cached and every time you load your page its being fetched again and takes time. Hope you got the point, to make it cache you can apply other techniques like you can create category page and cache that page where your required category can be shown etc. For further reading here is an other reply from Plugin author himself https://wordpress.org/support/topic/plugin-w3-total-cache-cache-query-urls-disabled Hope that makes sense by now.