Hi James,
From reading your question, it sounds like you edited a file within the WordPress core install. Is that correct? If so, then, yes, your code will have been overwritten when you updated to WordPress 4.7.
Rather than edit core, it’s a better idea to edit your theme or child theme to achieve the functionality that you need or want. While I am not a developer, I would suggest that you could achieve the search limits by adding some variation of that code to the functions.php file within your theme or child theme.
Hi Liam, thanks for your reply.
Your right, the query.php does get overwritten when WP is updated, and it happened on the last update. But all I had to do then was quickly bring up query.php, make the change and the search was back to serching on the posts only.
But in this latest 4.7 update the query.php looks completely different and the code oulined above is not present anymore.
I am not a develper either. I had a quick look at my theme’s functions.php file but I didnt see anything obvoius related to search that I can edit. I guess I will have to keep searching for an answer.
Thank you for your help.
You want to (1) create a child theme and (2) use pre_get_posts in a filter function to restrict a query to just posts when appropriate. https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
Do not modify core files.
I had to search around a bit but I finally found the Answer.
All I had to do was add the following code snippet to my theme’s functions.php file.
————————————————-
function mySearchFilter($query) {
if ($query->is_search) {
$query->set(‘post_type’, ‘post’);
};
return $query;
};
add_filter(‘pre_get_posts’,’mySearchFilter’);
————————————————–
Once I added the code my search now only searched on posts, and it shouldnt get overwritten in the next WP update. Sorted 🙂
James.
Nicely done, James, nicely done.