Viewing 10 replies - 1 through 10 (of 10 total)
  • Moderator bcworkz

    (@bcworkz)

    You apparently have an empty value under post meta key ‘function_ospiti’ that shouldn’t be there. Because it’ll match '', the selected() function marks that option as selected and hides the desired “Persone” default value. Remove that empty value from post meta and your dropdown should appear normally again.

    Good catch, if you compare the dropdown which works -> https://prnt.sc/Cq30UdfzZYUI – with dropdown which does not work as wanted -> https://prnt.sc/KudnmIkVnAqX – it’s clear that there is empty option value and that is why specific dropdowns do not work as intended.

    Thread Starter sacconi

    (@sacconi)

    So I should remove '<option value="">' inside here:

    $functionOspitiDropdown .= '<option value="">' . esc_html__('n.Guests', 'sacconicase') . '</option>';
    Moderator bcworkz

    (@bcworkz)

    No, that part is OK, that’s the default value when nothing has yet been selected. There’s an empty value under post meta key ‘function_ospiti’ that shouldn’t be there. That’s in your database. I’m not sure how your meta box for this is configured, but where ever you’ve entered the other numbers, there’s an empty value that shouldn’t be there.

    Thread Starter sacconi

    (@sacconi)

    This is my meta box: https://pastebin.com/FHSzwP7j

    Moderator bcworkz

    (@bcworkz)

    You’d need to find the posts which have empty values for the dropdowns which are not displaying correctly and assign a numeric value instead of leaving the field blank. Probably something recently added?

    Maybe better would be to add code to the dropdown’s option loop to skip past any empty values so only numeric options are listed. Change line 7 of this code to this:

    if ( empty($_GET['function_ospiti']) continue;
    $number = $_GET['function_ospiti'];

    Untested, but should work.

    Ideally you ought to do both of the above suggestions because any posts with no value assigned will not be in filtered results even though they probably should be.

    Thread Starter sacconi

    (@sacconi)

    the new code broke the site ??

    Moderator bcworkz

    (@bcworkz)

    I’m traveling and can’t check the code until next week.

    Moderator bcworkz

    (@bcworkz)

    I’m back home now. I didn’t correctly think through what’s really going on with my previous code suggestion. Leave line 7 as it is. Insert this line directly above it, below the foreach line:
    if ( empty( $value )) continue;

    I did test it this time, but I had to guess at what the query returns. I assumed it is an array of integers as string values. If correct, the code works on my test site, both with and without a previous selection.

    Thread Starter sacconi

    (@sacconi)

    Ok, this is working!

Viewing 10 replies - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.