Skip Navigation

[Resolved] Custom query filter with multiple conditions

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.

This topic contains 29 replies, has 4 voices.

Last updated by umbertoZ 1 month, 1 week ago.

Assigned support staff: Minesh.

Author
Posts
#1901113

Hi, I've a View that queries 4 different post-types, it is like a news stream. I'm using conditionals to display each post-type with a diffrent content template, but I've also to hide certain posts based on different criterias. I could do it with conditionals, but it is not working fine with pagination.

The View should load 25 posts on each page, but I'm displaying only some of them (because of conditionals) so I haven't 25 posts on each page as I would like. It would work fine with infinite scroll, but Views has the bug that it doesn't work properly if I include a Toolset form under each post (that is my case, this is the reported bug: https://toolset.com/forums/topic/toolset-forms-with-same-id-in-a-view-with-infinite-scroll/).

So, I suppose the solution is to set up something with wpv_filter_query. Can you help me?

I need to remove from the query all the posts that DOESN'T match this conditions:

( '[wpv-post-type]' eq 'follower' )
AND
( '[types field='followed-id' output='raw'][/types]' ne '[wpv-attribute name='wpvprofile']' )
AND
( '[types field='followed-id' output='raw'][/types]' ne '9764' )
AND
( '[wpv-current-user]' eq '[wpv-post-author format='meta' meta='user_login']' )

Can you give me an example of custom query filter to do it?

thanks

#1901253

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Can you please share problem URL and access details so I can check how you configure your view.

*** 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.

#1901915

Minesh
Supporter

Languages: English (English )

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

My IP is: 103.137.192.210

Please let me know once you grant the access.

#1903485

Minesh
Supporter

Languages: English (English )

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

I've added the following code to the "Custom Code" section offered by Toolset with "Toolset custom code" snippet:
=> hidden link

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

Can you please confirm it works as expected?

#1903667

Hi Minesh, thenk you for the code. I think it is not working properly. One of the conditions is that the author of the post has to be the current user, I suppose this line is where you set it: 'author'=>$current_user->ID,

but if I check the stream on the frontend I still see a result like this:

------STREAM-POST------
post-type: follower

post-id: 19434
post-author: marc
followed-id: 19466

------------

'19466' ne '164' AND '19466' ne '9764' AND 'umberto' eq 'marc'
----------------

It should not be there, because user 'umberto' is not equal to 'marc'.

can you check it again?

thanks

#1903673

Minesh
Supporter

Languages: English (English )

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

Sure, it looks like my IP is changed to: 103.82.145.167

Can you please whitelist it and I would be happy to check.

#1903701

Ok, but I cannot do it now, I would in a couple of hours, I will ping you when it's ready.

#1904479

Hey, you can access the website.

#1905337

Hi Minesh, I hope you can access the website, if not, tell me your new IP.

cheers

#1906361

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

Hello Umberto, Minesh is not available for a couple of days, he will be back on Monday.

I can take a look at this issue, but I am afraid my IP will change before your next reply. So, I suggest that you create a Duplicator copy of your website and share a download link for it. Either I or Minesh will build the copy locally and work on it. Your next reply will be private to let you share the download link safely.

#1906867

Hi, we removed the IP restriction, so now you can login and work on the dev site.

thanks

#1907329

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

Thank you! But, unfortunately, the credentials shared in your message of January 11th at 3:02 pm are not working for me, can you double-check from your side and update that message or include the credentials in your next message.

#1908017

Jamal
Supporter

Languages: English (English ) French (Français )

Timezone: Africa/Casablanca (GMT+01:00)

I have activated views debugging to check out what the view is doing but for some reason, the debug popup never opens and we can see some debug information at the bottom of the page. hidden link

So, I thought to create new elements, that can be minimal, until we find out what to do then we can test again on the /home/ page. I created the following elementos:
- "JTS Home" page. Similar to the /home/ page.
- "JTS Member Home" view. Similar to the _member_home view. I did not use a content template for it.
- "JTS User Following Log" view. Similar to the "User - Following Log" view.

I updated the custom code to be applied for this view too.

// changing line 4 to
    if ( in_array($view_id, array( 9023, 19926 ) ) ) {

Regarding your message at (January 13, 2021 at 10:21 am), you said that that post should not appear there, but I do not see why, because the conditions are built using "AND", the user marc is not equal to umberto, which will evaluate the nested conditions to false. And thus, it DOES'nt match the condition.
Maybe we are missing something! As far as I can tell:
"NOT( A AND B AND C AND D)" is equivalent to ((NOT A) OR (NOT B) OR (NOT C) OR (NOT D)) which evaluate to false in that case.

Are we missing something? Can we work on the new views I created until we get this working for you?

#1908149

Hello Jamal... this is Marc, owner of the project you are assisting with. Thank you for helping figure out this tricky member home page issue.

I wanted to mention that the location of the debug.log is here
/wh-files/debug.log

I use wh-files instead of wp-content. I cleared out the log as it was quite full.

Let me know if you need anything else, however umberto is the lead developer and has the best understanding of the code.

marc

#1908291

Hi Jamal, in the original view we display or not some post entry of the loop using conditionals. This creates problems with the pagination, because we don't get the same number of items on each page.

To prevent this behaviour we asked you to help us with a custom query to filter out those posts, so we can remove conditionals.

I need to remove from the query all the posts that DOESN'T match this conditions:

( '[wpv-post-type]' eq 'follower' )
AND
( '[types field='followed-id' output='raw'][/types]' ne '[wpv-attribute name='wpvprofile']' )
AND
( '[types field='followed-id' output='raw'][/types]' ne '9764' )
AND
( '[wpv-current-user]' eq '[wpv-post-author format='meta' meta='user_login']' )

Minesh custom query is not filtering the last condition.
I've added <ol> list on your view, so now you can check that the 4th item display this:

------STREAM-POST------
post-type: follower

post-id: 19779
post-author: umberto2
followed-id: 12124
------------
'12124' ne '164' AND '12124' ne '9764' AND 'umberto' eq 'umberto2'

It should be filtered by the custom query, because ('umberto' eq 'umberto2) is false, so the post should not be there.

I hope now it's clearer. I can see the View debug window.

cheers