Skip Navigation

[Resolved] Custom query filter with multiple conditions

This support ticket is created 3 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

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.

Our next available supporter will start replying to tickets in about 2.48 hours from now. Thank you for your understanding.

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 29 replies, has 4 voices.

Last updated by umbertoZ 3 years, 2 months ago.

Assisted by: Minesh.

Author
Posts
#1910531

Minesh
Supporter

Languages: English (English )

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

I see you are having really complex structure where you used many nested views with shortcode attribute and many [wpv-conditional] statements.

As you can see with my previous reply:
=> https://toolset.com/forums/topic/custom-query-filter-with-multiple-conditions/#post-1903485

I exclude the posts from the view ID 9023 where author equal to current loggedin user and followed-id not in $wpvprofile (the shortcode attribute you pass) and 9764. Is this not correct?

Can you please tell me where exactly to what content template or view you added the following view?
=> hidden link

I tried to search but i do not able to locate the above view within any content template or another view.

#1910575

Hi Minish, first of all thanks for your patience and great job, I know it is a very complex View, actually we asked help to try to remove some conditionals and make it simpler.

You can find the view ID 9023 in content template _member_home on line 117:

/wp-admin/admin.php?page=ct-editor&ct_id=8556&action=edit

We use it on page /home/ with this shortcode:

[wpv-post-body view_template="_member_home" item="[types usermeta='user-profile-id' output='raw' current_user='true'][/types]"]

You asked me:

I exclude the posts from the view ID 9023 where:
author equal to current loggedin user
and
followed-id not in $wpvprofile (the shortcode attribute you pass) and 9764.

Is this not correct?

No, it is not correct, the first condition has to be "author DIFFERENT to current loggedin user" to exclude all posts of other authors and display only current user posts.

cheers

#1910693

Minesh
Supporter

Languages: English (English )

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

Ok - thank you.

I see the following view is assigned to multiple post types.
=> hidden link

That means you want to display only loggedin user posts belongs to all post types the view is assigned to - correct?

#1911131

Hi, the view is assigned to multiple post types, but the custom filter should work only on post-type = 'follower'.

I'm displaying all the posts where followed-id = $wpvprofile, but in the case
followed-id != $wpvprofile
AND
followed-id != 9764,
I only want to diaplay posts where the current-user = post-author.

I hope it's clearer.

However, I've to say that if I could use AJAX infinite scroll in this View, I wouldn't need this custom query... (reported bug: https://toolset.com/forums/topic/toolset-forms-with-same-id-in-a-view-with-infinite-scroll/).

thanks

#1912123

Minesh
Supporter

Languages: English (English )

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

Can you please check now: hidden link

I've adjusted the code as given under to the "Custom Code" section offered by Toolset with "toolset-custom-code" snippet.

function func_remove_unwanted_posts( $query_args ,$view_settings, $view_id ) {
    global $post;
    global $current_user; 
    global $WP_Views;
  
    if ( in_array($view_id, array( 9023, 19926, 18389) ) ) {
      
      $wpvprofile = do_shortcode("[wpv-attribute name='wpvprofile']");
      
      $custom_query = new WP_Query( 
 						array(
  							'post_type' => 'follower',
  							'posts_per_page' => -1,
                  			'author__in'=>array($current_user->ID),
                            'fields'=>"ids",
                  			'meta_query' => array(
                          			'relation'=>"AND",
        								array(
                                        'key'     => 'wpcf-followed-id',
                                        'value'   => array($wpvprofile, 9764),
                                        'compare' => 'NOT IN'),
                  						array(
            								'key'     => 'wpcf-followed-id',
            								'compare' => 'EXISTS'),
    									)
                ));
      
      $found_posts = $custom_query->posts;
      
        
        
      $query_args['post__in'] = $found_posts;
      }
    
    return $query_args;
}
add_filter( 'wpv_filter_query', 'func_remove_unwanted_posts', 10, 3);

I hope this time it works as expected.

#1912149

Hi Minesh, it seems it is excluding all the posts of the other post-types (Reports, Stream Comments, Experiences) and also all the posts where followed-id = wpvprofile. I want to display them.

I think it if filtering correctly the followed-id != wpvprofile posts, it is displaying only the current user posts, but it should not remove all the others.

cheers

#1912993

Minesh
Supporter

Languages: English (English )

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

So, that means,

For the post type 'follower', you want to display post where:
- post type = follower
And - followed-id != $wpvprofile AND followed-id != 9764,
And - the current-user = post-author.

This is what we have for now and this is fetching the correct results - right?

Now, for other post types 'report' 'stream-comment' and 'experience', what should be the condition:
- What conditions should apply for above post types?
- followed-id equal to $wpvprofile?
- And is there any other condition?

#1913025

Hi, in the case of 'stream-comment' and 'experience' we display all of them.

In the case of 'report', we display only reports where:

post type = report
AND
current-user != post-author
AND
post-author login_name != 'sitehost'

It means we want to exclude posts from the current user and and from 'sitehost' user.

cheers

#1913085

Minesh
Supporter

Languages: English (English )

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

So lets finally summarize this.

We should display all posts where following conditions match.

For the post type 'follower', you want to display post where:
- post type = follower
- And - followed-id != $wpvprofile AND followed-id != 9764,
- And - the current-user = post-author

For the post types 'stream-comment' and 'experience', you want to display ALL post, there is no condition applies - right?

In the case of 'report', we display only reports where:
post type = report
AND
current-user != post-author
AND
post-author login_name != 'sitehost'
It means we want to exclude posts from the current user and and from 'sitehost' user.

Is this correct now?

#1913107

Yes!!!

Thank you again for your patience.

#1913119

Minesh
Supporter

Languages: English (English )

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

So, I've adjusted the wpv_filter_query hook as given under which is added at "Custom Code" section of Toolset:

function func_remove_unwanted_posts( $query_args ,$view_settings, $view_id ) {
    global $post;
    global $current_user; 
    global $WP_Views;
  
    if ( in_array($view_id, array( 9023, 19926, 18389) ) ) {
      
      
      $wpvprofile = do_shortcode("[wpv-attribute name='wpvprofile']");
      
      /// getting follower posts
      $cq_follower = new WP_Query( 
 						array(
  							'post_type' => array('follower'),
  							'posts_per_page' => -1,
                  			'author__in'=>array($current_user->ID),
                            'fields'=>"ids",
                  			'meta_query' => array(
                          			'relation'=>"AND",
        								array(
                                        'key'     => 'wpcf-followed-id',
                                        'value'   => array($wpvprofile, 9764),
                                        'compare' => 'NOT IN'),
                  						array(
            								'key'     => 'wpcf-followed-id',
            								'compare' => 'EXISTS'),
    									)
                ));
      
      
      $found_follower = $cq_follower->posts;
      
      /// getting 'stream-comment','experience' posts
      $cq_get_all = new WP_Query( 
 						array(
  							'post_type' => array('stream-comment','experience'),
  							'posts_per_page' => -1,
                  			'fields'=>"ids",
                  			
                		));
      
      $found_all = $cq_get_all->posts;
      
      /// getting report posts
      $site_host_user = get_user_by('login', 'sitehost');
	  $cq_report = new WP_Query( 
 						array(
  							'post_type' => array('report'),
  							'posts_per_page' => -1,
                  			'author__not_in'=>array($current_user->ID,$site_host_user->ID),
                            'fields'=>"ids"
                  			
                ));
       $found_report = $cq_report->posts;
        
       $query_args['post__in'] = array_merge($found_follower,$found_all,$found_report);
      
    }
    
    return $query_args;
}
add_filter( 'wpv_filter_query', 'func_remove_unwanted_posts', 10, 3);

Can you please confirm now it works as expected?
=> hidden link

#1913133

It seems it works great!!

I will test it deeply.

Just another condition I haven't mentioned.

Reports are connected to a User Profile and to a Condition. The User Profile is also connected by a many-to-many relationship with Conditions. On the intermediary post I store a custom field to check if this relationship is Private or Not.

If it is Private and don't want to display the report connected to that User Profile and that Condition.

I can do it by a Conditional that uses a secondary view to display or not a report in the stream.
The view is a loop of Intermediary posts of the relationship User Profile-Conditions, filtered by:

Select items with field: Keep it private is a string equal to 1
AND
Filter post title by a search term set by the View shortcode attribute: title

I pass the attribute title in this way in the Stream log View (if post-type 'report'):

title="Users Conditions: [wpv-post-id item='@user-report.parent'] - [wpv-post-id item='@condition-report.parent']"

The view shortcode is this:

[wpv-view name='user-following-log-condition-public-private' title="Users Conditions: [wpv-post-id item='@user-report.parent'] - [wpv-post-id item='@condition-report.parent']" ]

The output is the string:

'private': if the view found items

'public': if the view DOESN'T found items

So I can check it in a conditional.

Is it possible to use it in the custom query to exclude private reports? i think the only way is by a do_shortcode of the public/private View.

cheers

#1913173

Minesh
Supporter

Languages: English (English )

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

As acknowledged before, you have really complicated setup and No, there is no easy way or even if we will try to query it, we will have too many queries and that will result in performance and even that is also a custom programming.

I can tell you what hooks you can use, in this case we are using 'wpv_filter_query' and I already demonstrated you how you can hook in custom queries. You can add additional custom queries as per your requirement on your own as this is custom code which is the beyond the scope of our support policy.

However, I already took step further and wrote few queries for you. You should also check the following Doc:
=> https://toolset.com/documentation/customizing-sites-using-php/post-relationships-api/how-to-migrate-your-site-to-new-post-relationships/

As you original issue is already addressed, I request you to mark resolve this ticket and please kindly open a new ticket with every new question you may have. This will help other users searching on the forum as well as help us to write correct problem resolution summery for original issue reported.

Thank you for understanding and really happy to see your really complicated set issue resolved.

#1917763

My issue is resolved now. Thank you!

#1929483

Hi Minesh, I found a problem with the custom code.

In the case of 'follower' CPT, the code is getting only posts where the author is the current_user. I need to exclude the other author's posts only when 'followed-id' filed value is DIFFERENT form 'wpvprofile' attribute.

I try to better explain the scenario.
Each user has a 'profile' CPT post asigned. Then I have the 'follower' CPT with the custom post field 'followed-id'.

Everytime a user starts following another user, I create a 'follower' post where the user who FOLLOW is the author and the FOLLOWED profile ID is stored in the 'followed-id' field.

On my stream I display activity from all the followed users, from the current user and from followers when the start following. I could display 3 different kind of messages:

1- CURRENT-USER begun following USER-44

2- USER-44 begun following USER-99

3- USER-55 begun following CURRENT-USER

What I don't want to show is the number 2. It is an activity form a user that the current user is following (so it is correctly quried by the View), but I want to remove it from the query, because it is not related to the current user.

So, the problem with the custom code is that it is excluding all the posts where the current user is not the author, but I need exclude them only when 'followed-id' is DIFFERENT from 'wpvprofile'.

I hope you can help me.

thanks

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.