Leave a Reply

2 comments

  1. Well, perhaps the answer could not be found in the WP_Query class for what I was trying to accomplish. Below is the final code that I plugged in to my functions.php file. I ultimately ended up search the WP_Query Class found in the wp_includes/query.php file. What I discovered is that there are a number of filter access points in which one can jump in and modify the query. I found that there was posts_search filter that allowed me to modify the search part of the MySQL query that was being sent to the database. There was an even more inclusive posts_where filter that allowed me to modify more of the MySQL query.

    Thanks to @Scribu over on Google Groups for his recommendation to do a var_dump on the incoming query to see if the filter I was using allowed me to access the part of the query that I wanted to modify.

    I ultimately decided however to access the all powerful posts_request filter which gave me the full MySQL query string and allowed me to pull out the core of the query request and replace according to one of the three different queries that I was needing to perform. I trimmed and stored in variables the beginning and ending of the query so as to not kill the query’s ability to be set dynamically.

    Here is the search form:

    function my_search_form( $form ) {
        (isset($_REQUEST['type']))? $ic_sType = $_REQUEST['type'] : $ic_sType = '';
    
    
        $ic_selectOpts = array(
            'all' =>'General Search [search full site contents]',
            'name'=>'Mineral Name [search the approved name for the mineral]',
            'detail'=>'Description [search the description of the mineral]',
            'loc'=>'Locality [search by mine name, district, state, or country]'
        );
    
        $form = '<form role="search" method="get" id="top_nav_search" action="' . home_url( '/' ) . '" >
        <div>
    
    
            <label class="screen-reader-text" for="type">' . __('Type of Search:') . '</label>
    
            <select name="type" id="my_search_type" >';
            foreach($my_selectOpts as $myso_key => $myso_val){
                $form .= "<option value='$myso_key'";
                if($myso_key === $my_sType) $form .= "selected='selected'";
                $form .= ">$myso_val</option>";
            }
    
            $form .= '</select>
            <label class="screen-reader-text" for="s">' . __('Search Form:') . '</label>
            <input type="text" value="' . get_search_query() . '" name="s" id="s" />
            <input type="submit" id="searchsubmit" value="'. esc_attr__('Search') .'" />
        </div>
        </form>';
    
        return $form;
    }
    
    add_filter( 'get_search_form', 'my_search_form' );
    

    And here is the search results filter:

    function my_search_results($query){
    
        if(isset($_REQUEST['type']) && isset($_REQUEST['s']) && ($_REQUEST['type'] !== 'all')){
    
            $myType = $_REQUEST['type'];
            $mySearch = $_REQUEST['s'];
    
            $qBegin = str_replace(strstr($query, "FROM" ), '', $query). ' FROM';
    
            //echo '<br>the qBegin string: '. $qBegin;
    
            $qEnd = strrchr($query, "ORDER BY" );
            //echo '<br>the qEnd string:' . $qEnd;
    
            switch ($myType){
    
                case 'name': //Title search
    
                    $query = $qBegin. ' wp_posts WHERE 1=1 AND wp_posts.post_title LIKE "%'.$icSearch.'%" AND wp_posts.post_type = "mineral" AND (wp_posts.post_password = "") AND(wp_posts.post_status = "publish") '.$qEnd;
    
                    break;
    
                case 'loc': //Location Search
    
                    $query = $qBegin. ' wp_posts,wp_postmeta WHERE wp_posts.ID = wp_postmeta.post_id AND 1=1 AND wp_postmeta.meta_key = "_location_meta" AND wp_postmeta.meta_value LIKE "%'.$icSearch.'%" AND wp_posts.post_type = "mineral" AND (wp_posts.post_password = "") AND(wp_posts.post_status = "publish") '.$qEnd;
    
                    break;
    
                case 'detail': //Details Search
    
                    $query = $qBegin. ' wp_posts WHERE 1=1 AND wp_posts.post_content LIKE "%'.$icSearch.'%" AND wp_posts.post_type = "mineral" AND (wp_posts.post_password = "") AND(wp_posts.post_status = "publish") '.$qEnd;
    
                    break;
    
                default: 
    
                    break;
            }
    
        } 
    
        return $query;
    
    }
    add_filter( 'posts_request', 'my_search_results');
    

    Hopefully this helps someone else.

  2. you use action hook to format the wp_query before execution take a look at
    Action reference pre get post
    you need to put your custom field filter options in [meta_query] for more detail on that check out wp_query codex function documentation.

    modified example from codex:

        function meta_add( &$query ) {
        if ( /*search result page */ ) {
            $query->set( 'meta_query', array(/*assoc of the filter */) );
        }
    }
    add_action( 'pre_get_posts', 'meta_add' );
    

    hope that helps you out