Exclude pages from WordPress search result page

Add this to your child themes functions file using a code editor like Notepad++. You will need to change the page I.D’s in the code to your own. Exclude Specific Pages From Search Results add_filter( ‘pre_get_posts’, ‘exclude_pages_search_when_logged_in’ ); function exclude_pages_search_when_logged_in($query) { if ( $query->is_search && is_user_logged_in() ) $query->set( ‘post__not_in’, array( 1, 2, 3, 4, 5 … Read more

Search only blog posts (default WP search widget)

@PieterGoosen has a good description on why your pre_get_posts callback is giving you a problem. Here’s an alternative workaround to restrict the native search widget to the post post type: /** * Restrict native search widgets to the ‘post’ post type */ add_filter( ‘widget_title’, function( $title, $instance, $id_base ) { // Target the search base … Read more

Search that will look in custom field, post title and post content

First, don’t use query_posts. Second, you can pass an s parameter to get most of the way there. $program_search=”test”; $args = array( ‘post_type’ => ‘program’, ‘s’ => $program_search, ‘meta_query’ => array( array( ‘key’ => ‘keywords’, ‘value’ => $program_search, ‘compare’ => ‘LIKE’ ), ) ); $t = new WP_Query($args); var_dump($t->request); That s parameter kicks the ordinary … Read more

Using standard search function with custom post type

Ok, so I did a little more digging around and it turns out to be pretty easy. I just needed to add a hidden input into the search form. Posting this here for anyone else looking for the answer: <form class=”search” action=”<?php echo home_url( “https://wordpress.stackexchange.com/” ); ?>”> <input type=”search” name=”s” placeholder=”Search&hellip;”> <input type=”submit” value=”Search”> <input … Read more

How to make dynamically-generated content searchable in WordPress?

The wp_posts table has a post_content_filtered column that plugins can use to cache expensive post content filters. The idea is that when you display the page, you don’t read post_content but you read post_content_filtered. This is nice, but it won’t solve your search problem because WordPress by default only looks at post_content and post_title. You … Read more

Why have my custom post type searches stopped working after changing URLs / updating WordPress?

It’s possible that, after moving the site, you hadn’t updated WordPress’ understanding of where the site is located. You might want to try adding the following to your wp-config.php file: define(‘WP_SITEURL’, ‘http://www.seriouslyfish.com’); define(‘WP_HOME’, ‘http://www.seriouslyfish.com’); This will override the settings in the database. WordPress uses the location settings to direct rewrite traffic, so this might solve … Read more

Order Search Results Page by meta_value If no Value Return Remaining Results

You can accomplish this with multiple meta queries. $query->set( ‘meta_query’, [ ‘relation’ => ‘OR’, ‘wpcf-start-date’ => [ ‘key’ => ‘wpcf-start-date’, ‘compare’ => ‘EXISTS’, ], ‘no-start-date’ => [ ‘key’ => ‘wpcf-start-date’, ‘compare’ => ‘NOT EXISTS’ ], ] ); $query->set( ‘orderby’, ‘wpcf-start-date’ ); $query->set( ‘order’, ‘ASC’ ); This will tell WP to create a query that will … Read more