Viewing 12 replies - 76 through 87 (of 87 total)
  • Moderator bcworkz

    (@bcworkz)

    Empty (but not null) DB value is fine as long as the record exists.

    Did you try:

    array(
            'key' => 'function_number',
            'value' => (int) $_GET['function_number'],
            'type' => 'numeric',
            'compare' => $_GET['function_number'] == '' ? 'EXISTS' : '<=',
        ),

    This appears to work in my limited testing. When no selection is made only posts with no function_number record at all are excluded. With a selection, only posts with values less than or equal to selection are returned.

    N.B: While it may not make sense for there to be a value argument when using EXISTS, one is required none the less. It doesn’t matter what the value is, but we must provide one.

    Thread Starter sacconi

    (@sacconi)

    Not working here, meybe my error is before, in this part?

    add_action( 'pre_get_posts', 'bedrooms_guests_search_query');
    
    function bedrooms_guests_search_query( $query ) {
       if (! is_admin() && $query->is_main_query()) {
          if ( array_key_exists('function_camere', $_GET ) &&
          
          array_key_exists('function_ospiti', $_GET ) && array_key_exists('function_bagni', $_GET )&& array_key_exists('function_number', $_GET ) ) {
    
    $meta_query = array(
        array(
    

    Moderator bcworkz

    (@bcworkz)

    That appears to be OK unless there’s a minor typo I’m not seeing.

    After this line $query->set("meta_query", $meta_query);, insert this:
    var_dump( $query->query_vars );
    Output will appear at the top of the results page. This will list all the criteria WP will use to compose a SQL query. Most will be empty, but what is not empty should reveal where the problem lies. Please show me this output.

    Once you obtain the information, you can comment out the var_dump() line so page appearance can return to normal.

    Thread Starter sacconi

    (@sacconi)

    I see only a part of the output because another part is under my sticky header. Here is what I can copy after I launch a search with no selection for all the selectors:

    [“attachment”]=> string(0) “” [“attachment_id”]=> int(0) [“name”]=> string(0) “” [“pagename”]=> string(0) “” [“page_id”]=> int(0) [“second”]=> string(0) “” [“minute”]=> string(0) “” [“hour”]=> string(0) “” [“day”]=> int(0) [“monthnum”]=> int(0) [“year”]=> int(0) [“w”]=> int(0) [“category_name”]=> string(0) “” [“tag”]=> string(0) “” [“tag_id”]=> string(0) “” [“author”]=> string(0) “” [“author_name”]=> string(0) “” [“feed”]=> string(0) “” [“tb”]=> string(0) “” [“paged”]=> int(0) [“meta_key”]=> string(0) “” [“meta_value”]=> string(0) “” [“preview”]=> string(0) “” [“s”]=> string(0) “” [“sentence”]=> string(0) “” [“title”]=> string(0) “” [“fields”]=> string(0) “” [“menu_order”]=> string(0) “” [“embed”]=> string(0) “” [“category__in”]=> array(0) { } [“category__not_in”]=> array(0) { } [“category__and”]=> array(0) { } [“post__in”]=> array(0) { } [“post__not_in”]=> array(0) { } [“post_name__in”]=> array(0) { } [“tag__in”]=> array(0) { } [“tag__not_in”]=> array(0) { } [“tag__and”]=> array(0) { } [“tag_slug__in”]=> array(0) { } [“tag_slug__and”]=> array(0) { } [“post_parent__in”]=> array(0) { } [“post_parent__not_in”]=> array(0) { } [“author__in”]=> array(0) { } [“author__not_in”]=> array(0) { } [“search_columns”]=> array(0) { } [“meta_query”]=> array(4) { [0]=> array(4) { [“key”]=> string(15) “function_ospiti” [“value”]=> int(0) [“type”]=> string(7) “numeric” [“compare”]=> string(2) “>=” } [1]=> array(4) { [“key”]=> string(15) “function_camere” [“value”]=> int(0) [“type”]=> string(7) “numeric” [“compare”]=> string(2) “>=” } [2]=> array(4) { [“key”]=> string(14) “function_bagni” [“value”]=> int(0) [“type”]=> string(7) “numeric” [“compare”]=> string(2) “>=” } [3]=> array(4) { [“key”]=> string(15) “function_number” [“value”]=> int(0) [“type”]=> string(7) “numeric” [“compare”]=> string(2) “<=” } } }

    Moderator bcworkz

    (@bcworkz)

    Appears to be the query vars when nothing is selected?
    “function_ospiti” [“value”]=> int(0)
    “function_camere” [“value”]=> int(0)
    “function_bagni” [“value”]=> int(0) etc…

    I suspect your code isn’t quite robust enough yet to handle such a situation. It requires a sort of exception handling (not a literal programmer’s “exception” though). That can be dealt with later. I suggest first getting proper results when all selections are made.

    It’s very difficult to scan for problems when the data is all run together like that. It should be better formatted of you look at the page’s source HTML view (Ctrl/Cmd + U in most browsers). You will then also be able to see everything.

    I see you left the var_dump() in place so I can see for myself (or I caught it before you removed it again). When all selections are made, it does appear to be working correctly? In a cursory check it appears all search results fit the criteria I selected. It does fail when no distance to sea is selected. The compare element is still “<=” instead of “EXISTS”. This is the first exception we should address.

    This should work:
    'compare' => $_GET['function_number'] == '' ? 'EXISTS' : '<=',
    It works correctly on my site at least.

    The other integer selections (persons, bedrooms, etc.) are probably OK as-is since the comparison is greater than or equal to 0. I think the taxonomy selections are OK as-is as well? Maybe it’s just this distance to sea that’s an issue with no selection.

    Thread Starter sacconi

    (@sacconi)

    Yes, the problem was only for the “distance from the sea”, when is not selected, but also with 'compare' => $_GET['function_number'] == '' ? 'EXISTS' : '<=', I still see this problem

    Thread Starter sacconi

    (@sacconi)

    I left the var_dump with 'compare' => $_GET['function_number'] == '' ? 'EXISTS' : '<=', so you can see it’s not working. The last selector still requires to be selected, otherwise no results even with the other selectors

    Moderator bcworkz

    (@bcworkz)

    Do it this way:
    'compare' => $_GET['function_number'] == 0 ? 'EXISTS' : '<=',
    ( equivalent to 0 instead of ”)

    Thread Starter sacconi

    (@sacconi)

    I cant see any change

    Moderator bcworkz

    (@bcworkz)

    It seems the EXISTS arg is unreliable. Lets revert the “compare” element to simply: 'compare' => '<=',

    And have the value set to a very large number when no selection is made:
    'value' => $_GET['function_number'] == 0 ? 9999 : $_GET['function_number'],

    Should return all properties with no distance or those < 10 km from sea.

    Thread Starter sacconi

    (@sacconi)

    Finally it’s working!

    Moderator bcworkz

    (@bcworkz)

    ?? ?? ?? ??

Viewing 12 replies - 76 through 87 (of 87 total)
  • The topic ‘Adding a search criterium in my custom search box’ is closed to new replies.