How do I make search results include ONLY pages, no posts?

You can enforce a post type per callback on pre_get_posts: is_admin() || add_action( ‘pre_get_posts’, function( \WP_Query $query ) { $post_type = filter_input( INPUT_GET, ‘post_type’, FILTER_SANITIZE_STRING ); if ( $post_type && $query->is_main_query() && $query->is_search() ) $query->set( ‘post_type’, [ $post_type ] ); }); If that still includes other post types, you have a second callback registered on … Read more

Search Issue in the theme

Every request sent to http://example.com/?s=string will result in a search query, WordPress will use the search.php to render the output. It doesn’t matter where the request is came from. If you need to do an AJAX search, you have to write your own function to do the search and then return it. I have an … Read more

Noindex, nofollow stuck on homepage

The setting is stored in the options table under the key blog_public, the value is either 0 or 1. You can see the value of all options by manually visiting the page /wp-admin/options.php. A quick way to get rid of it would be to remove the noindex action hooked to wp_head, which is what outputs … Read more

How to add WooCommerce SKU to search query?

If you are using wordpress search you can add this code to make it work function search_by_sku( $search, &$query_vars ) { global $wpdb; if(isset($query_vars->query[‘s’]) && !empty($query_vars->query[‘s’])){ $args = array( ‘posts_per_page’ => -1, ‘post_type’ => ‘product’, ‘meta_query’ => array( array( ‘key’ => ‘_sku’, ‘value’ => $query_vars->query[‘s’], ‘compare’ => ‘LIKE’ ) ) ); $posts = get_posts($args); if(empty($posts)) … Read more

Changing the search results destination URL

You can create a new page. Let’s say you want to have: http://example.com/mysearch/ Create a page that will have that URL structure. Next, Search form -> Go to and on you search form action do: <form role=”filter-search” method=”get” id=”sd_searchform_filter” action=”<?php echo home_url( ‘/mysearch/’ ); ?>”>… Now go to functions.php (or where you want this function) … Read more

Conditional tag search-no-results

There is no conditional tag for no results on a search page, but you can create yourself one. You basically just have to check the value of $wp_query->found_posts, if it is 0, returns false, any other value, returns true function is_search_has_results() { return 0 != $GLOBALS[‘wp_query’]->found_posts; }

Set default text for the_search_query();

Do not hard code a value in the search field. You will get a lot of nonsense searches because people do not expect that they have to clean up the field before they can actually type something into it. They will just type and hit enter. Alternatives: Use the placeholder attribute. You can style it … Read more

Separating search results collectively by type

There’s two options. The first and probably the easiest option is to run the query twice on the page in each place you want to use it. // Author Block while(have_posts()) : the_post(); if ( ‘book-author’ == get_post_type() ) { ?> <h2>AUTHOR</h2> <?php the_title(); } endwhile; rewind_posts(); // Book Block while(have_posts()) : the_post(); if ( … Read more