WordPress knowledge sharing

WordPress search results remove short links generated by the Simple Urls plug-in

My friend's WordPress uses the simple urls plug-in to do the work from the external link to the internal link. It is really convenient, but there is a drawback. When searching for keywords in the blog, the short links generated by the simple urls plug-in will appear in the search results. Such search results are not what users want, so the user experience is poor. To avoid this problem, Weieis Blog found a piece of code provided by @ Wansi Blog (original website: http://yusi123.com/2038.html ), Yes Remove short links generated by the Simple Urls plug-in from wordpress search results

Just add the following code to functions.php to realize the above mentioned functions. In addition, ID and classification can be changed according to personal needs.

Exclude articles or pages with specific IDs. The code is as follows:

//Search results exclude articles or pages with specific IDs
function Bing_search_filter_id($query) {
if ( !$ query->is_admin && $query->is_search) {
$query->set(‘post__not_in’, array(40,819));// The ID of the article or page
}
return $query;
}
add_filter(‘pre_get_posts’,’Bing_search_filter_id’);

Tip: Modify the article or page ID as needed

Next is the article excluding some categories. The code is as follows:

//Search results exclude some classified articles
function Bing_search_filter_category( $query) {
if ( !$ query->is_admin && $query->is_search) {
$query->set(‘cat’,’-15,-57′); // The ID of the classification, which is preceded by a minus sign to indicate exclusion; If the ID is written directly, it means that only the ID is searched
}
return $query;
}
add_filter(‘pre_get_posts’,’Bing_search_filter_category’);

Note to modify the ID, see the note.

If you want to remove all short links generated by the Simple Urls plug-in, you can use the following code:

//Search results exclude all pages
function search_filter_page($query) {
if ($query->is_search) {
$query->set(‘post_type’, ‘post’);
}
return $query;
}
add_filter(‘pre_get_posts’,’search_filter_page’);

After the third code is set, Weieis Blog can completely remove all short links, and what users see is the real content of the article.

Through the above method, the short links generated by the Simple Urls plug-in in the wordpress search results are finally removed. Previously, my friend was annoyed by the short links in the search results, and has considered adding code manually to realize the function of external link to internal link. After using this code, the problem is finally solved.

Like( zero )
Article name: WordPress Search Results Remove Short Links Generated by Simple Urls Plug in
Article link: https://www.vpsss.net/6948.html
Copyright notice: The resources of this website are only for personal learning and exchange, and are not allowed to be reproduced and used for commercial purposes, otherwise, legal issues will be borne by yourself.
The copyright of the pictures belongs to their respective creators, and the picture watermark is for the purpose of preventing unscrupulous people from stealing the fruits of labor.