Displaying number of search results for each post type

First, your callback to pre_get_posts is wrong. That is not the way that set works. If you had debugging enabled, you’d see Notices. It should be: function more_posts_per_search_page( $query ) { if ( !is_admin() && $query->is_main_query() ) { if ( $query->is_search ) { $query->set(‘posts_per_page’,500); $query->set(‘post_type’,array( ‘author’, ‘book’)); } } } add_action( ‘pre_get_posts’,’more_posts_per_search_page’ ); Second, there … Read more

Change post order by meta key, per post basis

Don’t try to make WP_Query, or SQL, do this. In the backend, don’t save artist_last_name save something like artist_sort_name instead, and set it to either first or last name accordingly, then sort on that normalized key. To sort conditionally with WP_Query— that is, to ignore my advise above and try to make WP_Query/SQL do this– … Read more

pre_get_posts returns non property object when using posts__not_in

You are using pre_get_posts incorrectly. You should not be returning anything at all And you should be manipulating the query object that is passed into the filter and not modifying the globals at all. For example: function modify_onthisday_cat( $query ) { if ($query->is_category(7) && $query->is_main_query() ) { global $post; // not going to be set … Read more

Excluding a Custom Post Type with a specific tag using pre_get_posts

I used tax_query instead of tag__not_in and it works now. if ( is_post_type_archive( ‘event’ ) ) { $taxquery = array( array( ‘taxonomy’ => ‘event-tag’, ‘field’ => ‘id’, ‘terms’ => array( 53 ), //the ID of the event tag ‘operator’=> ‘NOT IN’ ) ); $query->set(‘tax_query’, $taxquery); return; }

pre_get_posts having no effect on my category archive

You’ve got a typo there, should be: is_admin(). You should enable debugging by setting define(‘WP_DEBUG’, true); in wp-config.php to see such errors. Also there’s no need of returning anything in action hook. add_action( ‘pre_get_posts’,’filter_archive’, 1 ); function filter_archive( $query ) { if ( ! is_admin() && is_category(’76’) && $query->is_main_query() ) $query->set(‘posts_per_page’, ’12’); } Sidenote: personally … Read more

How to reset usual $query on search page to push custom $wpdb query there?

This is not how pre_get_posts works. The pre_get_posts action gives developers access to the $query object by reference (any changes you make to $query are made directly to the original object – no return value is necessary). https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts What you are doing is simply wrong. You don’t “return” another query via pre_get_posts, you alter the … Read more

Search for two strings in WP search

I am not 100% sure what you are trying to accomplish, but you can “search for both strings” simply by concatenating them: function checkForAa($query){ if($query->is_search){ $search_string = $query->get( ‘s’ ); if( stripos( $search_string , ‘aa’ ) !== false){ $new_string = str_ireplace( ‘aa’ , ‘xxx’ , $search_string ); }elseif( stripos( $search_string , ‘å’ ) !== false){ … Read more