• Hi Guys,

    Can anyone help? Before the 4.7 update I adjusted the code in wp-icludes/query.php so that the search would only search on posts. Now after updating to 4.7, I can see that pages are being included again.

    With the previous version all I had to do was change some code in query.php and set it to search on posts only, changing the code $q[‘post_type’] = ‘any’; – to – $q[‘post_type’] = ‘post’;

    Looking at the updated query.php file now I dont see the code anywhere. Does anyone have any idea how I correct this so that it searches on posts only please?

    Thanks

    James

Viewing 5 replies - 1 through 5 (of 5 total)
  • 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.

    Thread Starter trader121

    (@trader121)

    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.

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    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.www.ads-software.com/Plugin_API/Action_Reference/pre_get_posts

    Do not modify core files.

    Thread Starter trader121

    (@trader121)

    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.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Search Posts only in v4.7’ is closed to new replies.