WordPress excludes the specified articles / categories from being displayed by on-site search

WordPress is the most used CMS program at present. As long as we need some functions, we can basically search for such article solutions. This is one of the reasons why many users like WordPress. Because some CMS seems good, but there are few technical documents to provide solutions, such as zblog program, we is no longer using it.

Today, when we meet a customer enterprise website, when we need to search the content, we can only specify the technical documents of the customer company, and the articles and content included in other pages will not appear in the search results. This problem has not been encountered before, so search for solutions. Here, the technical documents are sorted out so that they can be used directly when needed in the future.

First, exclude a category from being searched

function wp_ search_ filter_ category( $query) {
if ( !$query->is_ admin && $query->is_ search) {
The $query – & gt; set (‘cat ‘,’ – 12, – 13, – 14 ‘); / / – symbol indicates that this classification is excluded < br > from the list}
return $query;
}
add_ filter(‘pre_ get_ posts’,’wp_ search_ filter_ category’);

Here, according to our needs, we can write the excluded categories directly into the third line of code above.

Second, exclude specified articles from being displayed

function wp_ search_ filter_ id($query) {
if ( !$query->is_ admin && $query->is_ search) {
$query->set(‘post__ not_ In ‘, array (1,2,3)); / / enter the specified article ID < br > and}
return $query;
}
add_ filter(‘pre_ get_ posts’,’wp_ search_ filter_ id’);

It is excluded here that the specified article will not be displayed in the search results.

In summary, we will edit and add the code and corresponding ID to the current topic functions.php In effect in the document.

Related Article
Add article browsing history function to WordPress

This article was last updated on January 22, 2014. It has not been updated for more than three years. If Read more

Set the method for WordPress website to specify that the classified content will not be displayed on the home page

Sometimes, for the needs of specific content, we hope that the content of a certain category will not be displayed Read more

Remove the default link function of WordPress upload image

Our friends have found out when using WordPress program that if there is no manually set link for the uploaded Read more

WordPress common template function reference document

Basic template filefile namedescribe style.css Theme style file index.php Home page template file header.php Header template file single.php Single article Read more

Leave a Reply

Your email address will not be published. Required fields are marked *

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-spam image