1

I am using posts_join and posts_where filters to include a custom table in a query.

this page says I should "use whatever WordPress did to search the post title field to search my custom table fields (rather than trying to duplicate WordPress's rather complex logic)"

...but the code they provide doesn't work. The replace finds nothing and hence nothing is added.

    $where = preg_replace( "/\(\s*post_title\s+LIKE\s*(\'[^\']+\')\s*\)/", "(post_title LIKE $1) OR (wp_mytable.entry LIKE $1)", $where );

I used $wp_query->request to display the query and found that the keyword is wrapped in a checksum. They are autogenerated and different every time.

"SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts LEFT JOIN wp_xxxx_entries ON wp_posts.ID = wp_xxxx_entries.post_id WHERE 1=1 AND (((wp_posts.post_title LIKE '{b12c1eafdcf04fcb64989a2ad3879bd84b16fbffc9cb81dee32e0fce2cc68d5a}keyword{b12c1eafdcf04fcb64989a2ad3879bd84b16fbffc9cb81dee32e0fce2cc68d5a}') OR (wp_posts.post_excerpt LIKE '{b12c1eafdcf04fcb64989a2ad3879bd84b16fbffc9cb81dee32e0fce2cc68d5a}keyword{b12c1eafdcf04fcb64989a2ad3879bd84b16fbffc9cb81dee32e0fce2cc68d5a}') OR (wp_posts.post_content LIKE '{b12c1eafdcf04fcb64989a2ad3879bd84b16fbffc9cb81dee32e0fce2cc68d5a}keyword{b12c1eafdcf04fcb64989a2ad3879bd84b16fbffc9cb81dee32e0fce2cc68d5a}'))) AND wp_posts.post_type IN ('post', 'page', 'attachment') AND (wp_posts.post_status = 'publish' OR wp_posts.post_author = 1 AND wp_posts.post_status = 'private') OR (wp_xxxx_entries.entry LIKE '%keyword%') ORDER BY wp_posts.post_title LIKE '{b12c1eafdcf04fcb64989a2ad3879bd84b16fbffc9cb81dee32e0fce2cc68d5a}keyword{b12c1eafdcf04fcb64989a2ad3879bd84b16fbffc9cb81dee32e0fce2cc68d5a}' DESC, wp_posts.post_date DESC LIMIT 0, 10"

Do I need to replicate this? If so how?

  • "wrapped in a checksum" - that doesn't sound right. Can you show us an example please? – Rup Mar 21 at 19:35
  • thanks for your reply. I've added what is returned by $wp_query->request when i search with the default WP search box – guardiancrescent Mar 21 at 23:08
  • I'd expect that to be wp_posts.post_title LIKE '%keyword%', i.e. your checksum is where the %s should be. Is there any chance whatever mechanism you're using to view the SQL is substituting something for the %s? Does that actually work, i.e. does it actually return posts that match keyword? – Rup Mar 21 at 23:15
  • I notice wp_xxxx_entries.entry LIKE '%keyword%' has still got its %s. Where does that come from? Is there any chance whatever's adding that is breaking the rest of the SQL? – Rup Mar 21 at 23:17
  • it works, yes. That part is my code. I've added it with the posts_where filter. I have no idea where the checksum could be coming from. I'm working with a default install of Local on Mac with MySQL 5.7.23 – guardiancrescent Mar 21 at 23:21
0

It is literaly a place holder and not a checksum and it indicates what part of the query should be converted to % before executing the query. (this is done to harden security)

I do not see right now an easy way to fix the preg_replace that the codex suggests as I suspect this place holder might be "randomly" generated, but if you just want to search for the same term that is being searched for in the rest of the tables you can get the term from the WP_Query object and make the replacement code much easier to write as you will not need to extract the term from the query itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.