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?
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?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?