Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Greg Winiarski

    (@gwin)

    Hi,
    the Custom Fields extension does not allow right now to search by a third-party custom field (ie field not created with WP Adverts), to have a dropdown that will allow searching by a custom meta field you would need to custom code it https://wpadverts.com/doc/custom-fields-search-form/

    Thread Starter teeboy4real

    (@teeboy4real)

    thanks for the reply but im clueless with coding

    the post meta (rmp_avg_rating) values are 5, 4, 3, 2 and 1

    How will i display this in the code in the format

    VALUE = LABEL (displayed in dropdown)
    5 = 5 Star Rating
    4 = 4 Star Rating
    3 = 3 Star Rating
    2 = 2 Star Rating
    1 = 1 Star Rating

    hello
    I don’t think it can be in a custom field.
    Wouldn’t that be good?
    On the ad’s profile.

    advertisement details

    Plugin Author Greg Winiarski

    (@gwin)

    The whole code should look something like this

    
    add_filter( 'adverts_form_load', function( $form ) {
        if( $form['name'] != 'search' ) {
            return $form;
        }
        $form['field'][] = array(
            "name" => "rmp_avg_rating",
            "type" => "adverts_field_select",
            "order" => 20,
            "label" => __("Rating", "adverts"),
            "max_choices" => 1,
            "empty_option" => true,
            "empty_option_text" => "Select Rating",
            "options" => array(
                array( "value" => 1, "text" => "1 Star" ),
                array( "value" => 2, "text" => "2 Stars" ),
                array( "value" => 3, "text" => "3 Stars" ),
                array( "value" => 4, "text" => "4 Stars" ),
                array( "value" => 5, "text" => "5 Stars" ),
            ),
            "meta" => array(
                "search_group" => "visible",
                "search_type" => "half" 
            )
        );
        return $form;
    } );
    add_filter( 'adverts_list_query', function( $args ) {
        if( ! adverts_request( "rmp_avg_rating" ) ) {
            return $args;
        }
        $rating = adverts_request( "rmp_avg_rating" );
        $args["meta_query"]["rmp_avg_rating_min"] = array(
            "key" => "rmp_avg_rating",
            "value" => $rating,
            "type" => "NUMERIC"
        );
        return $args;
    } );
    

    although i am guessing the rmp_avg_rating is a floating-point number so the current search will find only the ratings that exactly match 1, 2, 3, 4 or 5.

    In this case, the search part would need to be something like

    
    $args["meta_query"]["rmp_avg_rating_min"] = array(
        "key" => "rmp_avg_rating",
        "value" => $rating,
        "compare" => ">=",
        "type" => "NUMERIC"
    );
    $args["meta_query"]["rmp_avg_rating_max"] = array(
        "key" => "rmp_avg_rating",
        "value" => $rating+1,
        "compare" => "<",
        "type" => "NUMERIC"
    );
    
    Thread Starter teeboy4real

    (@teeboy4real)

    Hello Greg

    The code worked perfectly thanks very much you are awesome!
    This second code was what I used

    $args["meta_query"]["rmp_avg_rating_min"] = array(
        "key" => "rmp_avg_rating",
        "value" => $rating,
        "compare" => ">=",
        "type" => "NUMERIC"
    );
    $args["meta_query"]["rmp_avg_rating_max"] = array(
        "key" => "rmp_avg_rating",
        "value" => $rating+1,
        "compare" => "<",
        "type" => "NUMERIC"
    );
    
    Plugin Author Greg Winiarski

    (@gwin)

    Hi,
    ok great, i am glad you have it resolved :), i am marking this thread as resolved hopefully someone in the future will find it useful as well.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Filter is search form by rating’ is closed to new replies.