So I've adjusted the code but the thing is that it will affect the performance as of now: hidden link
Its because you are having thousands of posts (38000+) and to loop through those posts to find the related child is available or not its causing the time and also you are using pagination.
But, if you dont want to compromise with performance then, for instance you are displaying 12 posts per page currently but out of those 12 posts if one of them is not having any child post then as we agree to not to display the post which does not have any child then there will be a blank spot remain. So it will display 11 result on the page.
If you want to compromise with performance then there is no change required but if you are happy that if there is n number of spots will be displayed empty for n number for posts where no child exists then let me know. I will change the code accordingly.
I see what you mean, it is taking a very long time to load but it is working perfectly now.
When you say to change the code and fix the performance issue, you mean that there will be empty boxes randomly wherever there should be a post, but is now hidden because there is an empty nested view in it? So for example some rows will have only 2 posts instead of 3 posts because of blank spots that are being hidden?
If that is the case, is there any other workaround? I do like having the columns in 3 I think it looks very nice. But if I change to only 6 posts per page will that help? Also, visitors will generally go straight to the search bar to search for a location, so it isn't necessary to automatically load all posts right away. Is there a way to limit initial amount of loaded posts to a certain number, and then only if a user searches, does it populate the results?
Or perhaps to "lazy load" the posts, that only when a user clicks on the next paginated button should it load?
When you say to change the code and fix the performance issue, you mean that there will be empty boxes randomly wherever there should be a post, but is now hidden because there is an empty nested view in it? So for example some rows will have only 2 posts instead of 3 posts because of blank spots that are being hidden?
==>
Yes, thats correct.
If that is the case, is there any other workaround? I do like having the columns in 3 I think it looks very nice. But if I change to only 6 posts per page will that help?
==>
No, there is no other workaround. Because if you do not want to display blank spots what will need to do is we need to first query all 35000+ posts and then need to check out of those 35000+ posts what posts having child posts, once we find what posts having child posts we will hook in those found posts.
But if you ready to negotiate with blank spots then we only need to deal with found 12 posts as per the pagination and it will be fast as we just need to find out of 12 posts what posts have child posts available. I hope this makes sense.
Also, visitors will generally go straight to the search bar to search for a location, so it isn't necessary to automatically load all posts right away. Is there a way to limit initial amount of loaded posts to a certain number, and then only if a user searches, does it populate the results?
==>
We are already doing that using pagination we are only loading the 12 posts by default but currently whats happening is before loading those 12 posts we are searching for all posts and find what posts having child posts out of those 35000+ posts, thats why its taking long time. Because even if you rule out Toolset and go with core WordPress if you try to query 35000+ posts will the extremely expensive in terms of performance.
So for instance, if you check the following page, page 2, it displays 9 records even though you set pagination of 12 posts per page because the 3 posts does not have child posts.
=> hidden link
The same way if you click on page 3, you will notice 8 results. There will not be blank spot on middle but at last.
Having said that, you set pagination for 12 but out of those 12 found posts only 5 posts have child then on that page it will only display 5 boxes instead of 12. If thats Ok for you its way far more better compare to the compromising with performance issue.
I checked, yes I see that, and the results are loading way faster now. I think this is the best solution and I am going to leave it like this for now- would it be possible to show me in the code what to change back to get the original full 12 results per each page? Just in case I decide to swap back.
If you want to switch to the another way, you can use the following code, it already been added to "Custom Code" section but the filter line is commented as you can see below:
//add_filter( 'wpv_filter_query', 'func_filter_child_none_post', 10, 3 );
function func_filter_child_none_post( $query, $view_settings, $view_id ) {
global $wpdb;
if ($view_id==1152) {
$sql = "SELECT ".$wpdb->prefix."posts.ID FROM ".$wpdb->prefix."posts
INNER JOIN ".$wpdb->prefix."postmeta AS tmapsmeta ON ( ".$wpdb->prefix."posts.ID = tmapsmeta.post_id )
LEFT JOIN ".$wpdb->prefix."toolset_maps_address_cache ON ".$wpdb->prefix."toolset_maps_address_cache.address_passed = tmapsmeta.meta_value
WHERE 1=1 AND ".$wpdb->prefix."posts.post_type = 'shiur-location' AND ((".$wpdb->prefix."posts.post_status = 'publish' OR ".$wpdb->prefix."posts.post_status = 'private')) GROUP BY ".$wpdb->prefix."posts.ID ORDER BY ".$wpdb->prefix."posts.post_date";
$res = $wpdb->get_col($sql);
$all_posts = $res;
foreach($res as $k=>$v):
$parent_ids = toolset_get_related_posts(
$v,
'shiur-location-to-shiur',
'parent',
1000000,0,
array(),
'post_id',
'child'
);
if(empty($parent_ids)){
unset($all_posts[$k]);
}
endforeach;
$query['post__in'] = $all_posts;
//$all_posts = array_values($all_posts);
//$query->posts = $all_posts;
//$query->found_posts = count($all_posts); // modify the count of found posts
// $query->post_count = count($all_posts); // modify the count of displayed posts
}
return $query;
}
Happy to help and glad to know that solution I shared help you to resolve your issue. Have a great time.