Solution to WordPress Media Library's failure to search for attachments

» WordPress » Solution to WordPress Media Library's failure to search for attachments

Modify a code to solve the problem that WordPress background media library cannot be searched

WordPress媒体库无法搜索附件的解决方法-极客公园

preface

Some time ago, a friend said that using Git would cause the background media library to be unable to search, because I am here to automatically rename uploaded files, so I am not too impressed with this need, but actually I have probably had an idea, so I solved this problem some time ago and recorded it here.

Problem source

In fact, the problem mainly comes from the function of [Search Results Exclusion Page]. When you search Baidu, you will find that the code is similar to 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');

This code means that if you search, only the post article type is displayed, that is, the article has no page, but the problem is that this code has no limited location, which makes it possible to run in the background, and the background search is generally article search, but if it is attachment search, it is GG, so we need to modify this code.

Modified code

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

The code modification is just to add an is_admin judgment, which is also very simple.

Postscript

Why is this simple problem almost undetected? First, Baidu's results are almost all wrong. In addition, no one has found this problem after the theme has been published for many years, which shows that this demand is very small. In fact, because Git automatically renames files, the method of searching file names is not a good attachment management method, The best way is to classify and label management. Cloudfall has recommended an attachment management plug-in for some time. Well, it's worth looking forward to.

--End--

Post reply

Your email address will not be disclosed. Required items have been used * tagging

One Reply to "WordPress Media Library cannot search for attachments"