Support » Plugins » Hacks » Code for searching posts?

  • Resolved stevewilson

    (@stevewilson)


    Does anyone know how to acquire the code behind the search box that searches posts, or itsequivalent? To make clear what I’m looking for, I would like to loop through all posts in our website’s database to find all graphics used over the years so as to eliminate all in our overstuffed library that are *not* in use, thereby to reduce storage and clutter.

Viewing 7 replies - 1 through 7 (of 7 total)
  • Moderator bcworkz

    (@bcworkz)

    I’m unclear how knowing the search code will help you achieve your goal, so I’m not sure what code you are looking for. Various snippets do different things along the way. Eventually a mySQL query results. If that is what you are looking for you can get it with the ‘posts_request’ filter.

    Your task sounds like something many site admins eventually need to do. Have you tried searching for a plugin that will clean “orphans” out of the media library?

    Would you mind sharing how you plan to accomplish this potentially formidable task?

    Thread Starter stevewilson

    (@stevewilson)

    When a criterian is entered in the search box on the page that lists all one’s posts, the unseen code rummages through the text of every post on the site. Since everything else is freely exposed, I thought someone might know how to get hold of that code as a huge shortcut which I could then modify to output to a file a list of found images to work with.

    I could right now enter something like “img” in that search box, but WP would simply list all the posts that have an image. It sounds like “post_request” would do the same. I’d need to open each post, find each “img” in the text, and create that list manually — which would be painfully inefficient.

    I had previously put this same request in the Plugins forum but got no reply (in 24 hours), so thanks for your doing so.

    Moderator bcworkz

    (@bcworkz)

    The ‘posts_request’ filter will pass the mySQL query that will be used for whatever the current request is. It can be used to run a query in phpMyAdmin or with $wpdb, but it seems to me it’d be just as easy to let it run as it normally would.

    I’ve considered a similar task but was daunted by the sheer tediousness of it all. I was hoping you had come up with some clever way to easily accomplish this 🙂

    I imagine a script that walks the uploads folders, compiling a list of all images. It then steps through the list, searching for each image in any post content, and if found, removing the image from the list.

    After the entire list is gone through, the remaining images should be orphans and are safe to delete. It is complicated by images where only the thumbnail or specific size is used; we shouldn’t want to delete the full and other sizes.

    It’s on my list of plugin ideas that I’ll probably never get around to. If anyone wants to develop my idea they are more than welcome to as long as the resulting code is GPL licensed 😉

    Thread Starter stevewilson

    (@stevewilson)

    Thanks for explaining that. Clearly I needed to learn about how posts_request works before making assumptions, which I’ll do. My problem is that, while I’ve done a fair amount of probing and recoding within the supplied WordPress code in order to modify the design of our website, I’ve done next to nothing outside it, such as working directly with phpMyAdmin. Gotta give it a try.

    Thanks for your good advice.

    Moderator bcworkz

    (@bcworkz)

    You’re welcome!

    recoding within the supplied WordPress code

    Really? That’s not a good idea and strongly discouraged by any legitimate WP dev. You will lose your changes when the related code is updated. And forgoing updates to preserve code is a huge security risk.

    You should be trying to achieve all customizing of your website through a child theme and/or custom plugin. And within those vehicles, any alteration of core or other functionality should be done via action and filter hooks, never by directly altering code you have not written yourself.

    It’s you site, you can do what you like with it if it’s legal for the server’s jurisdiction (IANAL), but the above is probably the best advice anyone can possibly give related to WordPress.

    Thread Starter stevewilson

    (@stevewilson)

    The re-coding was done maybe four years ago. We wanted to develop a non-blog look, more like a newspaper, given the nature of the site (see: http://letsfixthiscountry.org. Maybe things have changed, but at the time an extract or the full article was the only option. I think the extract permitted a single global setting as word count, etc.

    We added a column (or two) to make four and made changes that (1) permit each article to have its own length before the “read more” break to a single page is encountered (2) columns are flexible width and most important (3) articles can be assigned to different slots in the layout (4) they can be moved at will to different slots by changing a single background code (5) if no article is assigned to it, the layout slot doesn’t appear so that the layout itself can be varied into numerous configurations.

    It’s worked without a hitch. We documented all changes so that we can reinstate after updates — which we just did, also with no problems.

    Moderator bcworkz

    (@bcworkz)

    I’m quite sure child themes were around back then, but perhaps not as well documented and promoted. It’d have been easy to not know that they were an option. Additionally, some commercial themes are implemented as children of a common parent, which severely limits what one can do to customize since they essentially stole the child theme option away from their customers.

    If you’re content with reinstating changes every time, then that’s fine, but it’s probably not necessary. Your changes sound like mostly CSS and template changes, along with a few core code hooks that I know are available. It’d not be much more work to reinstate your changes as a child theme next time, then you’ll never have to re-code again.

    Whatever works for you, it’s all good 🙂

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Code for searching posts?’ is closed to new replies.