Unexpected esc_html and esc_attr behaviour
Use get_search_query or the_search_query to return/output the search term as a properly escaped value, don’t access $_GET directly. You’re trying to escape something that’s already URLencoded.
Use get_search_query or the_search_query to return/output the search term as a properly escaped value, don’t access $_GET directly. You’re trying to escape something that’s already URLencoded.
let’s start by defining an HTML search form: <form method=”get” action=”<?php bloginfo(‘url’); ?>”> <fieldset> <input type=”text” name=”s” value=”” placeholder=”search…” maxlength=”50″ required=”required” /> <p>Refine search to posts containing chosen tags:</p> <?php // generate list of categories $tags = get_categories(); foreach ($tags as $tag) { echo ‘<label>’, ‘<input type=”checkbox” name=”taglist[]” value=”‘, $tag->slug, ‘” /> ‘, $tag->name, “</label>\n”; … Read more
If you’re using FontAwesome, you can display an icon in the placeholder text like this : HTML <input placeholder=”” class=”fontAwesome”> CSS .fontAwesome{ font-family: ‘Helvetica’, FontAwesome, sans-serif; }
You need to make only one change in the form, change the first line to: <select name=”property_type[]” class=”form-control” multiple> Name of the field has to include [] to be considered as an array when it arrives on the server side. In the query string it will be listed as: property_type[]=condo&property_type[]=duplex And, on server side, when … Read more
You can query a list of post IDs using $wpdb based on their slugs, and then use that in your filter. Here’s a simple example of how to do this: function mm_search_filter( $query ) { // We will query a list of post IDs // by using $wpdb global $wpdb; $post_ids = $wpdb->get_results(“SELECT ID FROM … Read more
I think you have two issues that can be resolved in one go: ‘search’ => ‘*’ . esc_attr( $search_term ) . ‘*’, You don’t need the ‘*’s around the term. It looks like you’re trying to do a wildcard, but that’s what search already does by adding SQL ‘%’ around your term. Essentially, you end … Read more
In PHP, the number 0 is interpreted as false-ish. Your if-statement asks for a true-ish condition like this: If the result of the array_search function is interpreted as true-ish, THEN do the stuff which means if the result is 0, it is interpreted as false, and the code doesn’t run. What to do? Use the … Read more
did you try if (!isset($post_type)) or if(empty($post_type))?? For example I would try this: function mySearchFilter($query) { $post_type = $_GET[‘post_type’]; if ($query->is_search) { if (!empty($post_type)) { $query->set(‘post_type’, $post_type); } } return $query; } add_filter(‘pre_get_posts’,’mySearchFilter’); EDIT: Here is my working example code: Here is my CPT used for testing: add_action( ‘init’, ‘register_cpt_website_bookmarks’ ); function register_cpt_website_bookmarks() { $labels … Read more
There’s a separate Class as DB layer for this: The WP User Query Class. If you want to extend the search functionality, a possible option is to alter the query before you output the results on your search.php template. There’s a tutorial/how-to over here. If you need additional fields for your users, you can extend … Read more
By default it is usually posts and pages, but, you can modify this several ways URLs example.com/?s=test will search for the term test in posts and pages, but, example.com/?s=test&post_type=product will search posts of the type product for the term test. In the same way appending ?s=term to your post type archive URL will search that … Read more