Skip Navigation

[Resolved] Split: Adding outpts from two views – filter view with current page

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 9 replies, has 2 voices.

Last updated by Minesh 5 months, 3 weeks ago.

Assisted by: Minesh.

Author
Posts
#2699933

In page aa3 now there are three views. They are actually the same view we were working on (snippet) repeated, the only difference is that the first one displays the banner depending on the content of banner-position field: at the top of the page (position=1), the second in the middle (position=2) and the third one at the bottom (position=3). This filter works, no problem.

If I add a new filter "Banner page is a number equal to [wpv-post-id]" to let the banner display only if the page id is the same of what selected in the banner-page field (see the field group) the banner never shows even though the ID is the same. My question to you is: could the filter fail due to those annoying question marks appearing sometimes (see paragraphs) ? or any other hidden character ? how to fix this ?

I have added some more banners. Currently I applied the banner-page filter only to the mid position and nothing is displayed as you can see. Feel free to add the same filter to the other views and check.

Another strange thing: in the editor the output of the first view (the banner) is shown, the other two aren't, but they work on the page ....!?!?! see picture

#2699935

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

So, you want to filter the view result based on Banner Page custom field for all three views?
(top, middle and bottom)?

#2699936

Yes, to summarize: each banner must appear on each page only if:
- the page ID is the same of the one selected in banner-page content (ID) (filter, currently not working)
- the banner-position field content is the same of the banner position (1,2 or 3 -> filter, it works)
- today's date is between start date and end date (filter, it works)
- banner-address is inside 100km of current user position (distance filter, it works)

#2699939

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

As you confirm that you want to apply the Banner Page custom field filter for all three views
(top, middle and bottom) as well as you also want to filter the view result with custom fields like Banner Start, Banner End, Banner Position and Banner Page:
==>
I've adjusted the following code to "Custom Code" section under the code snippet "combine-nongeo-banners", So the modified code looks like as given under:

function func_combine_geo_and_non_geo_posts( $view_args, $view_settings, $view_id ){
    
    ///banner position
    //// 21628 = top [value=1],  21805 = middle [value=2], 21793 = bottom [value=3]
    $target_view_ids = array(21628,21805, 21793 );
    if ( in_array( $view_id, $target_view_ids ) ) { 
       global $post;
      
      $current_page_id = $post->ID;
      
      /// default banner position
      $banner_position = 1;
      if($view_id==21805){
        	$banner_position=2;
      }else if($view_id==21793){
        $banner_position=3;
      }
       
  /*   $view_args['meta_query'][] = array('key' => 'wpcf-banner-page',
                                          'value'=> $post->ID,
                                           'type'=>'NUMERIC',
                                           'compare'=> '=');
		
 */  
      

 /// finding non-geo addresses
$geo_result = array();
 $result = do_shortcode("[wpv-geolocation][wpv-view name='get-geolocated-banners' bannerposition='".$banner_position."' bannerpageid='".$current_page_id."' cached='off'][/wpv-geolocation]");
 $geo_result = explode(",",trim($result));
    
$non_geo_args = array(
                'post_type'        => 'banner',
                'fields'=>'ids',
                'post_status'      => 'publish',
                'numberposts'   => -1,
                 'meta_query'=>array(array('key'=>'wpcf-banner-address',
                                                              'value'=>''))
            );
            $non_geo_posts = get_posts( $non_geo_args );
 

          $combined_result =  array_merge($geo_result,$non_geo_posts);
          $view_args['post__in'] = $combined_result;  

    }
    return $view_args;
}
add_filter( 'wpv_filter_query', 'func_combine_geo_and_non_geo_posts', 99, 3 );

You should know this first and its very important that all views you added to the following page (top, middle, bottom) are act as placeholders.
- hidden link

Those views are NOT actually producing the results as we are overriding the the query argument for all those views (top, middle, bottom) using the view's filter code 'wpv_filter_query' that I shared above.

The results are produced using the view "get-geolocated-banners" as you can see with above code where are using using this view:

$result = do_shortcode("[wpv-geolocation][wpv-view name='get-geolocated-banners' bannerposition='".$banner_position."' bannerpageid='".$current_page_id."' cached='off'][/wpv-geolocation]");

The view "get-geolocated-banners" used to get the geo located posts before (which was your initial query) and now as you changed your requirement and want also filter with additional fields like Banner Start, Banner End, Banner Position and Banner Page custom fields.

As you can see on the view's edit page, I've added query filters for all those custom fields to "Query Filter" section:
- hidden link

Select items with field:
Banner start is a number lower than or equal TODAY()
AND
Banner end is a number greater than or equal TODAY()
AND
Banner page is a number equal to VIEW_PARAM(bannerpageid)
AND
Banner position is a string equal to VIEW_PARAM(bannerposition)

And we are passing the value as shortcode attribute for Banner Page (bannerpageid) and Banner position (bannerposition):

$result = do_shortcode("[wpv-geolocation][wpv-view name='get-geolocated-banners' bannerposition='".$banner_position."' bannerpageid='".$current_page_id."' cached='off'][/wpv-geolocation]");

So, if you want to add/remove any custom field filter, you should actually edit the following view and adjust the query filter for the same that actually produces the results and the found post IDs we pass to the placeholder views:
- hidden link

I hope now everything is sorted out in this matter and you are welcome to mark resolve this ticket:
- hidden link

#2700136
template.jpg

Minesh,
thank you so much for this, I just apologise because I didn't think that this banner thing could be so complicated and needed programming.... Before closing this ticked I do need to ask you why in get-geolocated-banners there is no loop template. I do need to change the banner size according to the user device. I was doing this easily with CSS in the loop template of original view Display Banner (see attachment) and I need to copy this code to get-geolocated-banners template but there is no box in the editor to do this. How can I create a loop template for get-geolocated-banners too ?
thanks

#2700146

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

This is the view ""get-geolocated-banners"":
- hidden link

That returns only post IDs as we are overriding the results generated by all those three views (top, middle, bottom).

You can build the view's loop as required using all those three (top, middle, bottom) views. You can add whatever things you want to display with those view's loop.

#2700204
banners.jpg

Minesh,
Got this thanks !
I did some more testing today and I noticed something I can't explain: some banners appear in the slider and they shouldn't while some which should show don't. Please see picture. I modified the page to show the get-geolocalized-banners output and I compared it with the banners actually shown and they do not match. AFA I've understood the view makes the query and apply filters, correct ? so I have removed the filters from the placeholder view (top banner, hid the other two), set the Post ID ordering (instead of random) but rotating banners aren't those listed by the view, please check, thanks

#2700287

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I've adjusted the code added to "Custom Code" section with code snippet "" as given under:


function func_combine_geo_and_non_geo_posts( $view_args, $view_settings, $view_id ){
     
    ///banner position
    //// 21628 = top [value=1],  21805 = middle [value=2], 21793 = bottom [value=3]
    $target_view_ids = array(21628,21805, 21793 );
    if ( in_array( $view_id, $target_view_ids ) ) { 
       global $post;
       
      $current_page_id = $post->ID;
       
      /// default banner position
      $banner_position = 1;
      if($view_id==21805){
            $banner_position=2;
      }else if($view_id==21793){
        $banner_position=3;
      }
        
        
 
 /// finding non-geo addresses
$geo_result = array();
 $result = do_shortcode("[wpv-geolocation][wpv-view name='get-geolocated-banners' bannerposition='".$banner_position."' bannerpageid='".$current_page_id."' cached='off'][/wpv-geolocation]");
 $geo_result = explode(",",trim($result));
     
$non_geo_args = array(
                'post_type'        => 'banner',
                'fields'=>'ids',
                'post_status'      => 'publish',
                'numberposts'   => -1,
                 'meta_query'=>array(array('key'=>'wpcf-banner-address',
                                           'value'=>''),
			             array('key' => 'wpcf-banner-page',
                                          'value'=> $current_page_id,
                                           'type'=>'NUMERIC',
                                           'compare'=> '='),
				      array('key' => 'wpcf-banner-start',
                                          'value'=> strtotime('today UTC'),
                                           'type'=>'NUMERIC',
                                           'compare'=> '<='),
					array('key' => 'wpcf-banner-end',
                                          'value'=> strtotime('today UTC'),
                                           'type'=>'NUMERIC',
                                           'compare'=> '>='),
										  
								)
            );
            $non_geo_posts = get_posts( $non_geo_args );
  
 
          $combined_result =  array_merge($geo_result,$non_geo_posts);
          $view_args['post__in'] = $combined_result;  
 
    }
    return $view_args;
}
add_filter( 'wpv_filter_query', 'func_combine_geo_and_non_geo_posts', 99, 3 );

As you changed the requirement we have to find the non-geo posts with the position of the view and banner position ID and start and end date. So I've added the arguments to the query filter:

$non_geo_args = array(
                'post_type'        => 'banner',
                'fields'=>'ids',
                'post_status'      => 'publish',
                'numberposts'   => -1,
                 'meta_query'=>array(array('key'=>'wpcf-banner-address',
                                           'value'=>''),
			             array('key' => 'wpcf-banner-page',
                                          'value'=> $current_page_id,
                                           'type'=>'NUMERIC',
                                           'compare'=> '='),
				      array('key' => 'wpcf-banner-start',
                                          'value'=> strtotime('today UTC'),
                                           'type'=>'NUMERIC',
                                           'compare'=> '<='),
					array('key' => 'wpcf-banner-end',
                                          'value'=> strtotime('today UTC'),
                                           'type'=>'NUMERIC',
                                           'compare'=> '>='),
										  
								)
            );
            $non_geo_posts = get_posts( $non_geo_args );

In addition to that - I would urge you to take it from here and find resolution for your query as this is total custom code based on your custom requirement. I already offer you the beyond and above help as as a supporter we can not build your business login and code your full requirement.

If you need any custom programming requirement, you can contact our certified partners who will help you with such custom programming help:
- https://toolset.com/contractors/

#2700430

Hello Minesh,
I don't understand why you moved the fiters to the code instead of simply create another view for not-geolocated banners and then merge the results. This was my original request and gives me more flexibility if I need to add more filters in the future (I don't now) without touching the code. I modified the snippet and it seems to work fine. I also read that there is a PHP function called Shuffle that shuffles an array that could be applied once the array is combined. I tried to add that line of code, but I commented it because it returns a bad SQL error, I don't know why. If you just can fix that to randomize the array ordering I think we are done. thanks

function func_combine_geo_and_non_geo_posts( $view_args, $view_settings, $view_id ){
      
    ///banner position
    //// 21628 = top [value=1],  21805 = middle [value=2], 21793 = bottom [value=3]
    $target_view_ids = array(21628,21805, 21793 );
    if ( in_array( $view_id, $target_view_ids ) ) { 
       global $post;
        
      $current_page_id = $post->ID;
        
      /// default banner position
      $banner_position = 1;
      if($view_id==21805){
            $banner_position=2;
      }else if($view_id==21793){
        $banner_position=3;
      }


$geo_result = array();
$result = do_shortcode("[wpv-geolocation][wpv-view name='get-geolocated-banners' bannerposition='".$banner_position."' bannerpageid='".$current_page_id."' cached='off'][/wpv-geolocation]");
$geo_result = explode(",",trim($result));

$nogeo_result = array();
$nogeoresult = do_shortcode("[wpv-view name='get-nongeolocated-banners' bannerposition='".$banner_position."' bannerpageid='".$current_page_id."' cached='off']");
$nogeo_result = explode(",",trim($nogeoresult));

$combined_result =  array_merge($geo_result,$nogeo_result);
///$combined_result = shuffle($combined_result);
$view_args['post__in'] = $combined_result;  

    }
    return $view_args;
}
add_filter( 'wpv_filter_query', 'func_combine_geo_and_non_geo_posts', 99, 3 );

#2700632

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I don't understand why you moved the fiters to the code instead of simply create another view for not-geolocated banners and then merge the results.
==>
Because the requirement from first initial post was to combine only two view results without having any extra query fitlers.

The way you did is also option and you can go ahead with that.

To shuffle the results you can use the following code:

function func_combine_geo_and_non_geo_posts( $view_args, $view_settings, $view_id ){
       
    ///banner position
    //// 21628 = top [value=1],  21805 = middle [value=2], 21793 = bottom [value=3]
    $target_view_ids = array(21628,21805, 21793 );
    if ( in_array( $view_id, $target_view_ids ) ) { 
       global $post;
         
      $current_page_id = $post->ID;
         
      /// default banner position
      $banner_position = 1;
      if($view_id==21805){
            $banner_position=2;
      }else if($view_id==21793){
        $banner_position=3;
      }
 
 
$geo_result = array();
$result = do_shortcode("[wpv-geolocation][wpv-view name='get-geolocated-banners' bannerposition='".$banner_position."' bannerpageid='".$current_page_id."' cached='off'][/wpv-geolocation]");
$geo_result = explode(",",trim($result));
 
$nogeo_result = array();
$nogeoresult = do_shortcode("[wpv-view name='get-nongeolocated-banners' bannerposition='".$banner_position."' bannerpageid='".$current_page_id."' cached='off']");
$nogeo_result = explode(",",trim($nogeoresult));
 
$combined_result =  array_merge($geo_result,$nogeo_result);

//shuffle the results 
shuffle($combined_result);

$view_args['post__in'] = $combined_result;  
 
    }
    return $view_args;
}
add_filter( 'wpv_filter_query', 'func_combine_geo_and_non_geo_posts', 99, 3 );