Home › Toolset Professional Support › [Resolved] Adding outpts from two views
This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.
Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.
Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|---|---|---|---|---|---|
- | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | 10:00 – 13:00 | - |
- | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | 14:00 – 18:00 | - |
Supporter timezone: Asia/Kolkata (GMT+05:30)
This topic contains 33 replies, has 3 voices.
Last updated by Minesh 6 months ago.
Assisted by: Minesh.
Hello all,
Following your tutorial here
https://toolset.com/course-lesson/creating-sliders-with-dynamic-post-content/
I have created a test banner slider that randomly rotates geolocalized banners (of local businesses located inside a 100km radius from the current user location). This works fine, but I'd like to add to the same slider other banners of businesses that don't need to be geolocated (country wide businesses), so that their banners are always included in the slider, no matter where the user is located. I could easily create another view for this filtering on the empty banner-address field, but I can't figure out how to put together the two views outputs on the same slider. I guess I should create the two views as classic, but then I'd loose the slider that currently uses a container block .... I am stuck, any clue ? I am open to any different solutions
Thanks
Regards
Nicola
Languages: English (English ) Spanish (Español )
Timezone: Europe/London (GMT+00:00)
Hi Nicola
The distance filter requires special handling, it isn't added like other filters like custom field filters, so it's not possible to combine it with other filters using an OR condition (posts should be within 100km or marked in some way as special), and I don't see any way to modify the query before the database is interrogated.
I think the solution will require the main query with the distance filter, then—in code—a custom query to get the other posts you want to include, which you would then merge with the initial results, before Views handles the results to create the slider.
You would need to use the wpv_filter_query_post_process hook: https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query_post_process
You will have access to the original results, will need to use get_posts to get the other results you want to include, and then you'll need to merge these with the original results (as well as update the arguments for the number of found posts).
That documentation page includes an example of using the filter.
I also found an old example of using the filter to display no posts until a search filter had been applied (redundant—it can be done within the UI), which I share below just so you have an idea of how the filter is used:
/** * No initial results * * Don't show View results until a filter has been applied * * Tests for custom field filters, taxonomy filters, or text searches */ function tssupp_no_initial_results( $query_results, $view_settings, $view_id ){ $target_views = array( 226 ); // Edit to add IDs of Views to add this to if ( in_array( $view_id, $target_views ) ) { // if there is a search term set if ( !isset( $query_results->query['meta_query'] ) && !isset( $query_results->query['tax_query'] ) && !isset( $query_results->query['s'] ) ) { $query_results->posts = array(); $query_results->post_count = 0; $query_results->found_posts = 0; } } return $query_results; } add_filter( 'wpv_filter_query_post_process', 'tssupp_no_initial_results', 10, 3 );
Hello Nigel,
thanks for this that I broadly understand, but goes beyond my tech skills, so I will need to find somebody to work on this. Just one question before closing this ticket: you say " I think the solution will require the main query with the distance filter, then—in code—a custom query to get the other posts you want to include ....". Why the second query needs to be coded ? can't it be just another view with no distance filter ?
thanks
Regards
Nicola
It needs another query to find banners that are always included in the slider and later we can combine the both results (banners that are always included and geolocated).
Hi Minesh,
Yes exactly, let's assume I have the second view as well (very easy, same as the first one without distance filter and an additional filter where banner-address = empty), my question is how to combine results ?
thanks
You can not merge view results as each view treated as single entity and you can not merge the results until and unless as shared we can use view's filter hook as shared by Nigel to merge the other result by querying the result using view's filter hook and merge the result.
Please let us know if you require further assistance to merge the results. If so, we will require problem URL and admin access details.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I have set the next reply to private which means only you and I have access to it.
To the "Custom Code" section offered by Toolset - I've added the "combine-nongeo-banners" code snippet and added the following hook to it:
- hidden link
function func_merge_non_geo_banners( $query_results, $view_settings, $view_id ){ $target_views = array( 21628 ); // Edit to add IDs of Views to add this to if ( in_array( $view_id, $target_views ) ) { $non_geo_banners = get_posts( array( 'post_type' => 'banner', // Types custom post type 'numberposts' => -1, 'meta_query' => array( array( 'key' => 'wpcf-banner-address', // Types Date custom field 'value' => '', 'compare' => '=')) )); $final_result = $query_results->posts + $non_geo_banners; $query_results->posts = $final_result; $query_results->post_count = count($final_result); $query_results->found_posts = count($final_result);; } return $query_results; } add_filter( 'wpv_filter_query_post_process', 'func_merge_non_geo_banners', 10, 3 );
As you can see - we query the non-geo posts and merge with the existing view result that displays the geo posts.
You will have to remove the view that displays the non-geo posts from the test page:
- hidden link
Can you confirm it works as expected now.
Hello Minesh,
I removed the second view, but I still see two siders, the first one rotetes ok, the second one is stuck on the orange banner.
Orange and yellow are supposed to rotate mixed with the others in the first slider. I guess that the stuck slider is due to the PREVIOUS button that is not visible. Actually I'd like to remove this button completely, I tried hiding it with CSS but the banner stops rotating.
Thanks for your help.
I removed the previous button now - can you please check now.
The button is gone, great thanks, but I still see two sliders, the second one is still stuck. The goal is to have just one slider with all banners together.
Do you mean there are two items displayed per slide and you want to display only one item at a time?
No, see picture. I want just one slider only rotating all banners (geolocalized+ not geolocalized)
Can you please check now: hidden link
I've adjusted the code added to "Custom Code" section with code snippet "combine-nongeo-banners" as given under:
function func_merge_non_geo_banners( $query_results, $view_settings, $view_id ){ $target_views = array( 21628 ); // Edit to add IDs of Views to add this to if ( in_array( $view_id, $target_views ) ) { $non_geo_banners = get_posts( array( 'post_type' => 'banner', // Types custom post type 'numberposts' => -1, 'meta_query' => array( array( 'key' => 'wpcf-banner-address', // Types Date custom field 'value' => '', 'compare' => '=')) )); $final_result = $query_results->posts + $non_geo_banners; $query_results->posts = $final_result; $query_results->found_posts = couninal_result);; $query_results->post_count = count($final_result); } return $query_results; } add_filter( 'wpv_filter_query_post_process', 'func_merge_non_geo_banners', 999, 3 );
Can you confirm it works as expected now.
Ok there is just one slider now. But reloading the page the blue banner appears first (normal size) then the green and purple banner start rotating (bigger size ?!?!?) and the blue banner never appears in the rotation anymore. Yellow and orange banners never show.
Please feel free to change banner addresses to some close where you live so that you can see the aa3 page. Or do you prefer I send you a video ?
Maybe its because you set the view to order by random order. I set the view to order by post ID and also changed the address to my address and I can see all banners are displayed.
Regarding reloading the page issue - I suggest you build your original constant first and then report issue if you have. As I can see you did not updated the current theme to latest version.
In addition to that - I see you are using optimization plugin, please check the following doc for more details:
- https://toolset.com/faq/how-to-use-optimization-plugins-with-toolset/