Adding a search criterium in my custom search box
-
Finally I got some more code to work on. I was trying to add a new search criterium to search my posts, the custom field meta key is “function_camere”, and I want to add another dropdown selector to choose a value for this new search criterium. I got a suggestion as follows on the link but this code broke my site, maybe have I to change something? https://wordpress.stackexchange.com/questions/421963/adding-another-search-field-in-a-custom-search-box/421976#421976
- This topic was modified 10 months, 1 week ago by sacconi.
The page I need help with: [log in to see the link]
-
Broke your site in what way? What errors were logged?
The shortcode does not work or submitting the form does not work? I’m pretty sure the wpb_demo_shortcode() function is OK, but I cannot fully test it to be sure since my site doesn’t have the same data as yours. I did change the tipologia taxonomy to something I do have and and the same for function_camere. With those changes everything works as expected on my site.
I seriously doubt the form submission would work correctly. I suspect WP does not know what to do with a “function_camere” query var. You likely need some “pre_get_posts” code to take the function_camere data and set it to appropriate query vars that WP understands such as “meta_key” and “meta_value”.
here is the error https://ibb.co/vqGz37w
Only the home page in german is visible normally (but without the new custom field), all the other pages have a “crazy” layout
If the new field is not appearing on your home page, there’s something wrong with the related code. It’s likely the same reason you get a crazy layout on other pages. The error is just manifesting itself in a different way due to differing context.
The code suggested by WordPress Buddah works correctly on my site with the minor changes I’ve mentioned before. Even when using the wrong meta key I still get valid output, except without any camere data in the dropdown. I suspect you’ve introduced an error while implementing the suggested code.
Be sure you only have one
wpb_demo_shortcode()
function declaration on your site and only oneadd_shortcode()
line for the [ricerca] shortcode.If that checks out, carefully compare your code with that suggested by WordPress Buddah. Theirs is working for me but not for you. Determine what’s different and correct.
Check a page’s source HTML code. After the cat and tipologia dropdowns (select/option structures) there should be a properly formed function_camere dropdown. It seems something has gone wrong here somewhere. What is wrong here might indicate what’s wrong with your code.
Last time I just added the new selector code in my current code, now I replaced the full code and I see the selector, but the function_camere selector is not filtering, you can test it in the search box: https://test.sacconicase.com/
Ather thing: I realised that the beginning of the new function is different than mine:
new function:
function wpb_demo_shortcode() { global $wpdb, $wp_query;
my function:
function wpb_demo_shortcode() { global $wp_query;
My custom search box keeps on being completely indipendent from other search functions in the website?
I realised that the selected value doesnt keep staying in the selector. Another question, how could I modify the code of the selector in order to have 2+, 3+, 4+…and so on instead of 2,3,4 …, I mean, when I select 3 I get in return all the apartment with 3 OR MORE bedrooms (3+)
For every
<option>
tag in the foreach loop you need to work in the selected() function so the saved selection appears as the current field selection. I recall you’ve used this in other select/option fields, where ever you’ve done this before can serve as a guide for what to do. As would some of the user contributed notes near the bottom of the above linked doc page.WordPress Buddah added $wpdb to the global declarations because they use this object to query for all the saved function_camare meta values. They also used the
DISTINCT
SQL statement in the query to ensure there are no duplicates in the returned values. There are no WP helper functions that would work this way.My custom search box keeps on being completely indipendent from other search functions in the website?
IDK. It depends on what code is used to process search requests. What happens probably depends on what field names appear in the request URL. Passing a “tipologia” value likely makes a different kind of search than if a “s” value were passed.
If you want a “+” to appear after all option labels in the foreach loop, concatenate it to whatever the current label string is. Like so:
esc_html($value).'+'
Have I to do something like
update_post_meta ( $post_id, "function_camere", $functionCamereValues );
replacing
update_post_meta
with something else? neither$post_id
seems to me correctAbout
esc_html($value).'+'
I dont need just this to appear (it’s easy, I’ve done it), but also a different filter behaviour, that is to say: when I select 4+ I get as return all the apartments with 4 or more bedrooms, so 5,6, and so onI cannot answer about update_post_meta() without more context. What’s the function where you’re updating post meta? What action or filter hook is used?
I thought it was odd the form’s results were already returning apartments with a greater number of bedrooms. Turns out it’s not ??
You’ll need to modify the form’s posts query through the “pre_get_posts” action hook. First confirm “function_camere” data exists, otherwise it’s the wrong query. You’ll need to properly set the typical meta_key and meta_value_num args. Additionally, set the “meta_compare” arg to “>=”.
However, if you have more than one meta data criteria for the query, you cannot use multiple meta_value_num args. Instead all meta data criteria needs to be encompassed within a single “meta_query” arg consisting of nested arrays. The outer array establishes AND or OR logic between the various meta_key criteria. Each inner array defines a specific criteria. Such as one array for the bedroom count and another for the person count.
In the WP_Query Custom Field section, refer to the last 3 examples in the section, starting with “Display posts from several custom fields:” Alter one of the examples to fit your specific needs. The very last example is likely more complex than what you need, one of the other two should be a better starting point.
the function is my search box:
function wpb_demo_shortcode() { global $wpdb, $wp_query; ...... ......
// 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__('n.Bedrooms', 'sacconicase') . '</option>'; foreach ($functionCamereValues as $value) { $functionCamereDropdown .= '<option value="' . esc_attr($value) . '">' . esc_html($value).'+' . '</option>'; } $functionCamereDropdown .= '</select>';
That function is to output the search form. There’s no reason to use update_post_meta() there. Searching is about existing data, there’s little reason to update anything as part of a search. At most someone could save chosen search terms for later reuse, but that would in a cookie or user meta, not post meta.
You apparently have a custom field in the post editor where a function_camere value can be entered for an apartment; and related code to save that value? That related code is where update_post_meta() would be used. If there is already data in place to search for, I’d think update_post_meta() is already in use.
I’m guessing your primary challenge now is to modify the search query to find all posts with equal or greater bedrooms of the selected number. That would be in a “pre_get_posts” callback or possibly other code that’s in use to process the form’s submitted data. Unless this other code is using get_posts() or a new WP_Query object, “pre_get_posts” is probably where you need to manage the bedroom count.
Yes, I have a custom field in the post editor for “function_camere”. But I cant understand why if I select ie “5+” and click “search” in my search box, the selection doesnt remain in the box. “pre_get_posts” is something to add in my search box code after the related selector? (for “function_camere”, or number of bedrooms)
Your form field selection does not persist because the code is lacking logic to assign the “selected” attribute to the correct option. You can use the selected() function to help with this. I’m sure you’ve used this in other fields elsewhere, use one of those as an example of what to do.
The “pre_get_posts” code modifies the search query so you get the expected results since default behavior isn’t desirable. This sort of code can reside in your theme’s functions.php. Do not try placing it in your form code because that only executes when the form loads, not when processing the search request.
I have something usefull in this code?
<select name="saldo" id="saldo"> <option value="option0" <?php selected( get_the_author_meta( 'saldo', $user->ID ), 'option0' ); ?>>-</option> <?php $arrival = esc_html__("balance: 15 days before arrival", 'sacconicase'); $selected_arrival = selected( get_the_author_meta( 'saldo', $user->ID ), "balance: 15 days before arrival"); echo "<option value=\"balance: 15 days before arrival\" $selected_arrival>$arrival</option>\n"; ?> </select>
The problem is that now I have an array of values in my function_camere selector, so I cant do as I did in my above code, I mean adding a $selected for each value manually,
This is the right idea, but with significant changes to fit the new context:
$selected_arrival = selected( get_the_author_meta( 'saldo', $user->ID ), "balance: 15 days before arrival"); echo "<option value=\"balance: 15 days before arrival\" $selected_arrival>$arrival</option>\n";
Revise
get_the_author_meta( 'saldo', $user->ID )
to get the selected field value that was saved. If an array comes back, extract out the appropriate array element that will be used to determine which option was selected.Revise
"balance: 15 days before arrival"
to correlate with the saved value that relates to the current option. For example, if “4” is what is actually saved, then for the 4+ option tag the second selected() arg should be “4”I don’t know how or where you’re saving user selections, so I cannot advise in any more specific detail.
This is how I store the values of “function_camere”:
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++ ) { echo '<option value="'.$i.'" '. selected( $number, $i, false ) .' >'.$i.'</option>'; } echo '</select></div>'; }
- The topic ‘Adding a search criterium in my custom search box’ is closed to new replies.