filter custom field values $min $max

The user needs to query posts by entering a $minPrice, $maxPrice and a category value.

Here is my query which returns no results:

Read More
function result() {

    $result = array(
        'cat'        => '10,17,18,13',
        'meta_query' => array(
            array(
                'key' => 'filter',
                'value' => array(  $min, $max ),
                'compare' => 'BETWEEN',
                'type' => 'numeric',
            )
        ),
    );

    // The Query
    $result = new WP_Query($result);
    // The Loop
    while ( $result->have_posts() ) : $result->the_post();
        include (ROCKABLE_INCLUDES . 'houseLandPackage-post-excerpt.php');

    endwhile;
    // Reset Post Data
    wp_reset_postdata();
}

So problem is im not getting any results back when i use $min and $max as the meta query values. I will get the correct “min – max” results back when i use “300” and “400” however.

Here is what the url looks like when the query is submitted:
http://localhost:8888/provgreen/?mode=advanced&min=3000&max=4000&s=+&cat=17&sa=Submit+Query

This thread states that the below code is needed for my query to work.:
http://wordpress.org/support/topic/searching-for-posts-with-custom-data-in-a-numeric-range-price?replies=4

Any help or pointers much appreciated.

Here is the code i have in search.php

<?php
    $selectedCategory='';
    $selectedPrice='';
    $newsearch='';

    $newsearch=$query_string;
    $newsearch.="&posts_per_page=8";
    $newsearch.="&post_type=post";

    if(isset($_GET["category"]) && $_GET["category"]!='') {
        $selectedCategory=$_GET["category"];
        $selectedCatId = get_term_by( 'slug', $selectedCategory, 'category' );
        $selectedCatId = $selectedCatId->term_id;
        $newsearch.="&cat=".$selectedCatId;
    }

    if(isset($_GET["filter"])&& $_GET["filter"]!='') {
        $selectedPrice=$_GET["filter"];
        $parts = explode("-",$selectedPrice);
        $min = $parts[0];
        $max = $parts[1];

        mam_custom_field_range('filter',$min,$max);
    }

    query_posts($newsearch);
?>

this is in function.php

// Define the function that will create the join string
function mam_custom_field_range($filter='',$min=false,$max=false) {
  global $mam_global_join,$wpdb;
  if ($filter && ($min !== false || $max !== false)) {
    $mam_global_join =
      " JOIN $wpdb->postmeta acfr_$filter ON
      ( $wpdb->posts.ID = acfr_$filter.post_id
        AND acfr_$filter.meta_key = '$filter'";
    if ($min !== false) $mam_global_join .= " AND acfr_$filter.meta_value >= $min";
    if ($max !== false) $mam_global_join .= " AND acfr_$filter.meta_value <= $max";
    $mam_global_join .= ')';
  }
}
// Define the join filter function
function mam_posts_join ($join) {
   global $mam_global_join;
   if ($mam_global_join) $join .= $mam_global_join;
   return $join;
}
// Add the filter to the hooks
add_filter('posts_join','mam_posts_join');

im using

<?php wp_dropdown_categories(); ?>

and some input fields for the min, max values in form.php

Related posts

Leave a Reply