[Resolved] Custom query filter with multiple conditions
This support ticket is created 3 years, 10 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.
No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.
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?
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.
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'.
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.
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.
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?
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.