Skip Navigation

[Resolved] Filter search results by User custom field

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to filter the results of a WordPress Archive based on a custom field in the current User's profile.

Solution:
Use conditional HTML to filter each item in the loop.

<wpv-loop>
[wpv-conditional if="( $(wpcf-my-select) eq 'abc' )"]
  [wpv-post-link] <br />
[/wpv-conditional]
</wpv-loop>

Relevant Documentation:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

This support ticket is created 6 years, 2 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.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by samC-4 6 years, 2 months ago.

Assisted by: Christian Cox.

Author
Posts
#612928

I am wanting to display an archive results page of a custom post type that only matches a members predefined fields they filled out on sign up. For example if a user signs up and has filled out the form on their profile that says "3 bedroom house" then I need the archive always match that users saved profile fields. I know I can do this with a custom search view but that is different, I need it to only display based on previously saved profile data.

is that possible? any help would be appreciated.

#613006

Hi, one way to do this is to use conditional HTML in the Loop Output editor of your WordPress Archive. For example, this code will hide all posts that do not have the value "abc" saved in the custom field "my-select":

<wpv-loop>
[wpv-conditional if="( $(wpcf-my-select) eq 'abc' )"]
  [wpv-post-link] <br />
[/wpv-conditional]
</wpv-loop>

This approach can be very effective but has some limitations. When you insert conditional code in an Archive, pagination can be impacted. The hidden posts still count towards the number of posts included on each "page" of results, so you may end up with fewer posts than expected on each page. If pagination is necessary for your archives, you should be aware of the challenges with this approach. We have more information about conditional HTML here:
https://toolset.com/documentation/user-guides/conditional-html-output-in-views/

Another option is to write custom code using the pre_get_posts API offered by WordPress. This API can allow you to apply custom filtering to an archive query using a meta query or tax query, or some other information: https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

User meta fields are stored in usermeta under a key name in the format "wpcf-" + field slug. So you can access a User field with the slug "birthdate" as "wpcf-birthdate" using the get_user_meta function:
https://codex.wordpress.org/Function_Reference/get_user_meta

#613091

thank you so much 🙂

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