Viewing 15 replies - 16 through 30 (of 87 total)
  • Moderator bcworkz

    (@bcworkz)

    That code generates the select field’s options. It doesn’t save anything. It’s typical that search request terms are not saved. In order to have the results page’s form reflect the earlier field selections you could get the value from $_GET['function_camere']
    As in:

    $number = empty($_GET['function_camere']) ? '': $_GET['function_camere'];
    echo '<option value="'.$i.'" '. selected( $number, $i, false ) .' >'.$i.'+</option>';
    Thread Starter sacconi

    (@sacconi)

    The above code made my site break

    Moderator bcworkz

    (@bcworkz)

    Huh, no errors in testing on my site only the code which I posted. There’s apparently an issue with how you included my code into your function. Please post your function’s code in its entirety, the version which includes my code so I can take a closer look at what you did.

    Thread Starter sacconi

    (@sacconi)

    Here: https://pastebin.com/LQGzzLW5

    But it wasnt clear to me if that code was to be put in the post custom field code or in the custom search form

    Moderator bcworkz

    (@bcworkz)

    OK, thanks. I’m sorry my intent was not clear. My suggestion was to modify your existing code, not replace it. Like this:

    function camere_meta_box_render( $post ){
    
        // Get the number and display it in a numeric field
        $number = get_post_meta( $post->ID, "function_camere", true );
        echo '<div><select name="function_camere">';
        for( $i = 0; $i < 13; $i++ ) {
           $number = empty($_GET['function_camere']) ? '': $_GET['function_camere'];
           echo '<option value="'.$i.'" '. selected( $number, $i, false ) .' >'.$i.'+</option>';
        }
        echo '</select></div>';
    }
    Thread Starter sacconi

    (@sacconi)

    I tryed this code but it seems I have to re-enter this data on thousands of apartments. Is this the only solution to coordinate the post custom box with the search box filter selector? Besides in the selector in the post editor I see numbers like 4+, 5+ or so on, this “+” should appear only in the search box..because when I launch a search for an apartment with 2 bedrooms, I get a 2+ result (I get apartments with 2 or more bedrooms)

    Thread Starter sacconi

    (@sacconi)

    Anyway the selection in the search box selector should stay fixed after I click on the search button, the same as for categories and taxonomies

    Moderator bcworkz

    (@bcworkz)

    There’s no need to alter the data for all the apartments. But the search query must correlate with that data. It’s easier but not essential that search field option value attributes also correlate with the apartment data. FYI, if you did need to alter all the saved values, it’s not too difficult with the Better Search and Replace plugin.

    What is currently saved for apartment bedrooms? An integer like 2 without the +? That would be ideal. The + should only occur in the select option label, the related value attribute should be just the integer, no +. (the options are currently this way, I’m restating for completeness)

    The search selections are not remaining after the search is run because the selected() code isn’t doing its job. I don’t know why. The search URL’s query string in part includes function_camere=2 or whatever the selected value was. I tested my suggested code on my site where the URL also includes function_camere=2 and the select field does reflect that selection. IOW the selected() code works as expected for me. This is independent of the saved values, all that matters is the query string value matches the option value.

    Thread Starter sacconi

    (@sacconi)

    Currently I have saved for function_camere an integer like 2 without the +

    If I delete the “+” in the code and have just integer numbers would I see again my previous selections in the post editor custom field? The integer+ “+” should be visible only in the search box selector, corresponding to a specific logic of search (showing the apartments with 2 OR MORE bedrooms (2+))

    Moderator bcworkz

    (@bcworkz)

    Understood. Let’s ignore that the results do not include apartments with more bedrooms for the moment. That’s for a different bit of code. Focusing on just the issue of the selection not staying after the form is submitted…

    When submitted, the new URL includes as part of the query string function_camere=2 or whatever the selection was. $_GET['function_camere'] will thus have the value of 2 in this example and assign it to `$number. When we do:
    selected( $number, $i, false )
    when $number and $i are the same ($i being the current option’s value), the function will return “selected=’selected'” to cause that option to appear as selected in the reloaded form. This behavior does not have anything to do with saved values, I don’t know why I even asked about it earlier. We don’t really need the get_post_meta() part. It also has nothing to do with the actual search results. The only factors are what’s in the URL and what selection is displayed on the form

    As I said this all works when I tested my suggested code on my site. You’ll need to debug what you have done to see where things have gone wrong. Start by, after submittal, checking the values for $number and $i for each iteration of the loop. They should only match once, indicating the correct selection.

    Thread Starter sacconi

    (@sacconi)

    I cant understand if it’s here I have to put the hands:

    // Retrieve unique values for 'function_camere' custom field
        $functionCamereValues = $wpdb->get_col("SELECT DISTINCT meta_value FROM $wpdb->postmeta WHERE meta_key = 'function_camere' ORDER BY meta_value");
        $functionCamereDropdown = '<select name="function_camere">';
        $functionCamereDropdown .= '<option value="">' . esc_html__('Select Function Camere', 'sacconicase') . '</option>';
        foreach ($functionCamereValues as $value) {
            $functionCamereDropdown .= '<option value="' . esc_attr($value) . '">' . esc_html($value) . '</option>';
        }
        $functionCamereDropdown .= '</select>';

    it’s the above code that runs the selector and the values in the selector in the custome search box

    Moderator bcworkz

    (@bcworkz)

    You’re not using the camere_meta_box_render() function? Or has it been re-written? I thought the code from this post was what you were using.

    Anyway, if you use a foreach loop instead of a for() loop my suggested loop code needs to change as well.

        foreach ($functionCamereValues as $value) {
            $number = empty($_GET['function_camere']) ? '': $_GET['function_camere'];
            $functionCamereDropdown .= '<option value="' . esc_attr($value) . '" '. 
    selected( $number, $value, false ) .' >' . esc_html($value) . '</option>';
        }
    Thread Starter sacconi

    (@sacconi)

    Ok your code now make the selected value remain the selector after pushing on button “search” https://test.sacconicase.com/?cat=165&tipologia=villa-a-schiera&function_ospiti=&function_camere=3&search=Cerca, but the search results are not affected by my choice. I mean, if I select 3 (bedrooms” I get back results also with 2 or 4 bedrooms, I should get an “n+” result, so that If I select 3 I get all the apartments with 3 bedrooms or more.

    Moderator bcworkz

    (@bcworkz)

    That was expected. One step at a time ?? How the form works and how the query works are two distinct parts of this puzzle.

    You’ve done nothing special to modify the query so far, right? You’re then relying upon the taxonomy slugs to act as query vars. That approach has been deprecated, but it still works so far. At some point it’s possible it’ll stop working. The following approach will not only get the results you want, but as a side effect correct for the deprecated approach currently being used.

    In order to have the query return apartments with equal or greater bedrooms you will need to modify the query vars through the “pre_get_posts” action hook. Your callback should verify the query is not an admin query, it’s for the correct post type, and that all the custom taxonomy query vars have values. Then you know you’ll be modifying the correct query and not one you shouldn’t modify. If it’s not the right query, do nothing.

    With the right query, construct a “tax_query” array using the custom taxonomy query var values. After doing so, unset the initial taxonomy query vars.

    The array you’ll construct will consist of an overall outer array with several inner arrays within. Each inner array will define criteria for one taxonomy. In the array for function_camere, you’ll set criteria to get posts with a term >= to the selection. The problem with tax_query is we cannot use arithmetic operators like >=. We only can use relational operators like IN, NOT IN, AND, etc. So for the terms argument, you’ll construct an array of all values >= to the selection and specify the “IN” operator. For example, if the selection is 3, you’d want the terms array to be [3, 4, 5, 6,] or up to whatever the maximum number of bedrooms there are. As long a a post has one of those terms assigned, it’ll be included in the search results.

    Aside from the outer array containing these inner arrays, it also accepts a “relation” argument. Here you can specify AND or OR logic be applied to all of the different taxonomy criteria. Using AND means fewer results since all the criteria must match. Using OR means more results since any one of the criteria could be a match.

    Thread Starter sacconi

    (@sacconi)

    where is the place I put this “pre_get_posts”? hook? I need only AND operators so that the search results are always the exact sum of my criteria

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