How can I enable a search form to search in custom meta?

Support General How can I enable a search form to search in custom meta?Resolved

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #38520
    sunil@sunil.co.nz[email protected]
    Participant

    I've created two cpt's. I've associated custom fields unique to both.

    Now I want to do a couple of things, starting with this.

    Allow the normal WordPress search form to include in it's search text that is within the custom fields.

    The idea is that if the user searches for a string, and that string is within custom field data, then WordPress's search results will indicate the posts that have that string within custom field data.

    WordPress does not do this by default.

    How can I do this?

    #38526
    Long NguyenLong Nguyen
    Moderator

    Hi,

    You can follow this article to use a third-party plugin to create an advanced search form and search posts by field value https://metabox.io/create-advanced-search-box-wordpress

    If you are familiar with coding, you can also follow this topic on WPSE https://wordpress.stackexchange.com/questions/165478/creating-a-search-form-for-custom-fields

    #38557
    sunil@sunil.co.nz[email protected]
    Participant

    Thank you Long.

    I was able to adapt the answers in that WPE thread to create a search that only returns results from a custom post type. Basically I created a custom search form and then I use pre_get_posts to alter the query.

    This leaves me with the other half of my challenge: allowing the search function to encompass data within custom fields. The article you've pointed to is good, but ultimately simply says to hand off that task to a third-party plugin. This isn't an option for me.

    Can you point to how people are doing this without plugins?

    From what I gather, I think I need to alter the search query further, also using pre_get_posts. But I haven't been able to find any documentation or articles on exactly how to do it.

    #38561
    sunil@sunil.co.nz[email protected]
    Participant

    Update:

    While I've been researching this issue I've found an issue with the terminology I'm using:

    I don't just want search results to encompass custom meta, I also need them to encompass custom taxonomies (I've just learnt that these things are not the same).

    So for a cpt of type 'book' with a custom taxonomy of type 'genre', I'd like a search that is limited to the cpt and that has the search string 'horror' return all results that have 'horror' attached as a value to the custom taxonomy 'genre'.

    #44120
    SridharSridhar
    Participant
Viewing 5 posts - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.