WordPress.org

Ready to get started?Download WordPress

Forums

filter custom field values $min $max (2 posts)

  1. dfhectik
    Member
    Posted 1 year ago #

    The user needs to query posts by entering a $minPrice, $maxPrice and a category value from a form. The data for the $min $max is entered into a custom field called "filter". And the value is a integer ranging from 3000 - 7000.

    Here is my query which returns no results:

    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

  2. vtxyzzy
    Member
    Posted 1 year ago #

    Since meta_query was introduced, the mam_custom_field_range function is no longer needed.

    Try changing this:

    function result() {
    
        $result = array(

    to this:

    function result() {
    
        if(isset($_GET["filter"])&& $_GET["filter"]!='') {
            $selectedPrice=$_GET["filter"];
            $parts = explode("-",$selectedPrice);
            $min = $parts[0];
            $max = $parts[1];
        } else {
            $min = 0; // Default min
            $max = 999999; // Default max
        }
    
        $result = array(

Topic Closed

This topic has been closed to new replies.

About this Topic