Skip Navigation

[Resolved] Text Search to include custom field

This support ticket is created 5 years, 4 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
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Karachi (GMT+05:00)

This topic contains 6 replies, has 2 voices.

Last updated by Waqar 5 years, 4 months ago.

Assisted by: Waqar.

Author
Posts
#1327697

Can I use the text search and return a custom field? What about snaging the main query and saying if the post type is X then return custom field as title (or include it).

#1327771

Hi Robina,

Thank you for contacting us and I'd be happy to assist.

> Can I use the text search and return a custom field?

- By default, when text search is used through Toolset Views, it only searches through post titles and content.
( ref: https://toolset.com/documentation/user-guides/front-page-filters/ )

But if your goal is to also include text from the custom fields, you'll need to use a third-party plugin "Relevanssi" along.
https://wordpress.org/plugins/relevanssi/

The detailed guide on using Relevanssi and Toolset together is available at:
https://toolset.com/documentation/user-guides/searching-texts-custom-fields-views-relevanssi/

> What about snaging the main query and saying if the post type is X then return custom field as title (or include it).

- You can use Views conditional output feature to achieve this:
https://toolset.com/documentation/user-guides/views-shortcodes/

For example, suppose you'd like to show text from a custom field "book-custom-field" as title, only for "books" post type and regular post title for all other types of posts.

In that case, you can add conditional blocks in your view like this:


[wpv-conditional if="( '[wpv-post-type]' eq 'books' )"] 
[types field='book-custom-field'][/types]
[/wpv-conditional]

[wpv-conditional if="( '[wpv-post-type]' ne 'books' )"] 
[wpv-post-title]
[/wpv-conditional]

To read about the available Toolset Views shortcodes, you can visit:
https://toolset.com/documentation/user-guides/views-shortcodes/

I hope this helps and please let me know if you need any further assistance around this.

regards,
Waqar

#1330649

Using Relevanssi is overkill. I don't need it for the entire site, just for this one view. It's simply a search box in my view listing and I am using the text search that should be looking at the custom field.

Conditional output would not work here. I am searching 1 post type already in the main view settings. So adding the conditional post type in the view loop would be moot and too late.

When I said that maybe we could snag the query, I mean that we hook into the main query and tell it that if we are searching a certain post type then we would add the custom field to the search parameter.

Kind of goofy that there is no way to adjust the search with add-in fields upon execution per view.

#1330653

Add to the conditional output suggestion...

This wouldn't work because the search wouldn't be looking at the custom field. So the results that we would be 'filtering' with conditional output would be incorrect anyway.

#1331063

Hi Robina,

Thanks for writing back.

When you write "if the post type is X" and "if we are searching a certain post type", it leads me to believe that the view is set to show the results from more than one post types. But the recent message also includes "I am searching 1 post type already in the main view settings", which suggests that it is a view to show only one post type.

Can you please share temporary admin login details, so that I can see how the view is set up and also share the exact requirement to change the view's query?

Your next reply will be private and though no changes will be made on your website, please make a complete backup copy, before sharing the access details.

Important note: To programmatically alter the specific view's query, the "wpv_filter_query" filter can be used:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

regards,
Waqar

#1333943

Love that. Let me give it a try first.

#1334785

Hi Robina,

You're welcome and please let me know if you need any assistance around this.

For a new question/concern, please open a new ticket.

regards,
Waqar