WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Custom Fields Search (33 posts)

  1. thebraindonor
    Member
    Posted 5 years ago #

    This exact problem was brought to my attention last month by a client of mine who does web design. He had a client that needed the WordPress search to include custom fields in the search page.

    I've posted the full details on my blog:
    WordPress Custom Field Search Plugin

    I wanted to make sure to post my solution here since many of the posts did help me out as I worked on my solution.

  2. mariostella
    Member
    Posted 5 years ago #

    @ thebraindonor

    Thanks for the link, but this does not really solve the issue of having a search engine that uses certain meta field values to narrow down the search following user-selected combo boxes and check boxes. Or at least I am unable to see how it could.

  3. thebraindonor
    Member
    Posted 5 years ago #

    @mariostella

    The code I provided can be used as a starting point to provide the functionality you are looking for. The goal of the search updates I made were look for matches that found in a meta field or the post content.

    In order to use the meta fields to narrow down the search, you would need to update the plugin code I provided to include an additional AND clause before the full query is assembled and returned:

    $term = $wpdb->escape($var_q);
    if (!$_GET['sentense'] && Count($search_terms) > 1 && $search_terms[0] != $var_q) {
        $query .= " OR ($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";
        $query .= " OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}')";
    }
    
    //begin new code
    if ($_GET['limit_author']) {
        $limit_author = $_GET['limit_author'];
        $query .= " AND (";
        $query .= "($wpdb->postmeta.meta_key = 'author')";
        $query .= " AND ($wpdb->postmeta.meta_value  LIKE '{$n}{$limit_author}{$n}')";
        $query .= ") ";
    }
    //end new code
    
    if (!empty($query)) {
        $where = " AND ({$query}) AND ($wpdb->posts.post_status = 'publish') ";
    }

    I've not tested it, but it should give you an idea of how and where to update the query.

Topic Closed

This topic has been closed to new replies.

About this Topic