• I’m trying to do a simple price range search for a custom post type, but after several hours am having no luck.

    In my form there are 4 fields. The normal search field, a hidden field with post type, a price low field and a price_high field. A search form outputs the following a url that looks the following. “?s=Keyword&post_type=properties&price_low=200&price_high=400”
    I’ve used the code below in the functions.php file based on what I can find in the wordpress docs and a few other sites, but nothing I try will get it to work.

    
    function searchfilter($query) {
        if ($query->is_search && !is_admin() ) {
            if(isset($_GET['post_type'])) {
                $type = $_GET['post_type'];
                if($type == 'properties') {
                    $query->set('post_type',array('properties'));
                }
            }
        };
        if($query->is_main_query()
        && isset( $_GET['price_low'] )
        && isset( $_GET['price_high'] ) ) {
            $meta_query = array(
                array(
                    'key' => 'cost',
                    'value' => array( $_GET['price_low'], $_GET['price_high'] ),
                    'type' => 'numeric',
                    'compare' => 'BETWEEN'
                )
            );
            $query->set( 'meta_query', $meta_query );
        }
    
    return $query;
    }
    add_filter('pre_get_posts','searchfilter');
    

    It brings up no results no matter what I put in the fields.
    If I remove the “&price_low=200&price_high=400” from the end of the url it works although missing the price filter. If I remove one of the price values it also just does brings up the results but again not up to or above the set value.
    I’ve also tried keeping them in separate functions, with no difference in the results.
    $wp_query->request produces the following with the low price and high price.

    SELECT SQL_CALC_FOUND_ROWS DISTINCT wp_posts.ID FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) LEFT JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id WHERE 1=1 AND (((wp_posts.post_title LIKE ‘%cardiff%’) OR (wp_postmeta.meta_value LIKE ‘%cardiff%’) OR (wp_posts.post_excerpt LIKE ‘%cardiff%’) OR (wp_posts.post_content LIKE ‘%cardiff%’))) AND ( ( wp_postmeta.meta_key = ‘cost’ AND CAST(wp_postmeta.meta_value AS SIGNED) BETWEEN ‘150’ AND ‘500’ ) ) AND wp_posts.post_type = ‘properties’ AND (wp_posts.post_status = ‘publish’ OR wp_posts.post_status = ‘acf-disabled’ OR wp_posts.post_status = ‘private’) GROUP BY wp_posts.ID ORDER BY wp_posts.post_title LIKE ‘%cardiff%’ DESC, wp_posts.post_date DESC LIMIT 0, 10

    And this when just doing the search with the keyword.

    SELECT SQL_CALC_FOUND_ROWS DISTINCT wp_posts.ID FROM wp_posts LEFT JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id WHERE 1=1 AND (((wp_posts.post_title LIKE ‘%cardiff%’) OR (wp_postmeta.meta_value LIKE ‘%cardiff%’) OR (wp_posts.post_excerpt LIKE ‘%cardiff%’) OR (wp_posts.post_content LIKE ‘%cardiff%’))) AND wp_posts.post_type = ‘properties’ AND (wp_posts.post_status = ‘publish’ OR wp_posts.post_status = ‘acf-disabled’ OR wp_posts.post_status = ‘private’) ORDER BY wp_posts.post_title LIKE ‘%cardiff%’ DESC, wp_posts.post_date DESC LIMIT 0, 10

    • This topic was modified 7 years, 5 months ago by Legin1976.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Hello,

    try to cast your values types (from $_GET) to int, eg.

    $low = intval($_GET['price_low']);
    $high = intval($_GET['price_high']);

    Also you always should filter data for security reasons.

    Thread Starter Legin1976

    (@legin1976)

    Hi..
    I’ve changed it to show the following but it doesn’t appear to have made any difference.
    It still gives unfiltered result and only ones when only one of the high or low values is in the url.

    if($query->is_main_query()
    && isset( $_GET['price_low'] )
    && isset( $_GET['price_high'] ) ) {
    $low = intval($_GET['price_low']);
    $high = intval($_GET['price_high']);
    	$meta_query = array(
    		array(
    			'key' => 'cost',
    			'value' => array( $low, $high ),
    			'type' => 'numeric',
    			'compare' => 'BETWEEN'
    		)
    	);
    	$query->set( 'meta_query', $meta_query );
    }
    • This reply was modified 7 years, 5 months ago by Legin1976.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Search between two newmeric values with a custom post type’ is closed to new replies.