Skip Navigation

[Resuelto] Filter search results by User custom field

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

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 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.68 hours from now. Thank you for your understanding.

This topic contains 2 respuestas, has 2 mensajes.

Last updated by samC-4 hace 3 años, 8 meses.

Assigned support staff: Christian Cox.

Autor
Mensajes
#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 🙂