0

I have a WordPress website that grabs product data from a custom table. I would like to be able to also search from fields in the custom table. Could someone provide a sample query of what this would look like? I cannot join the custom table to the posts table because the custom table has data that is not associated with any other database tables (unless I am misunderstanding joins). The end goal is to show all posts and custom table data that match the specified "s" variables data.

Thanks!

UPDATE

Here is the requested table structure of the custom table:

enter image description here

Just to clarify, I am trying to modify the WordPress search so that it searches the posts table using a like query. I also need it to return results that match from the custom table as well. I will need it to search the subarticle and Article fields of the custom table.

2
  • 1
    I am sure I could make a UNION work but I am a bit puzzled by what the search results would look like if nothing is related to the posts themselves.
    – s_ha_dum
    May 21, 2014 at 0:29
  • Maybe file an edit and show us how the custom table is structured.
    – kaiser
    May 21, 2014 at 1:22

1 Answer 1

0

I managed to solve this by taking s_ha_dum's suggestion to use a UNION. It is as follows:

(SELECT ID, post_status, post_title, post_excerpt, post_content FROM wp_posts WHERE ((`post_title` LIKE '%diamond%') OR (`post_excerpt` LIKE '%diamond%') OR (`post_content` LIKE '%diamond%')) AND (`post_status` = 'publish' )) UNION (SELECT ref, StoreID, Article, subarticle, description FROM wp_hwproducts WHERE (`article` LIKE '%diamond%') OR (`subarticle` LIKE '%diamond%') OR (`description` LIKE '%diamond%')) ORDER BY `post_status` DESC;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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