I am trying to modify a plugin that searches the posts table and have it also search the meta_value in the postmeta table.

original code is:

if (!($index_page = $wpdb->get_row("SELECT ID AS id, post_name AS slug FROM {$wpdb->posts} WHERE (post_content LIKE '%[" . W2DC_MAIN_SHORTCODE . "]%' OR post_content LIKE '%[" . W2DC_MAIN_SHORTCODE . " %') AND post_status = 'publish' AND post_type = 'page' LIMIT 1", ARRAY_A)))

and my code so far is:

if (!($index_page = $wpdb->get_row("SELECT {$wpdb->posts}.ID AS id, {$wpdb->posts}.post_name AS slug FROM {$wpdb->posts} INNER JOIN {$wpdb->postmeta} ON ({$wpdb->posts}.ID = {$wpdb->postmeta}.post_id) WHERE 1 = 1 AND (({$wpdb->postmeta}.meta_value LIKE '%[" . W2DC_MAIN_SHORTCODE . "]%') OR  ({$wpdb->posts}.post_content LIKE '%[" . W2DC_MAIN_SHORTCODE . "]%')) AND {$wpdb->posts}.post_status = 'publish' AND {$wpdb->posts}.post_type = 'page' AND (({$wpdb->postmeta}.metakey = '_content_field_6') OR ({$wpdb->postmeta}.meta_key = '_content_field_11'))LIMIT 1", ARRAY_A)))

but its not working and i cannot figure out why... any help appreciated.

1 Answer 1


You've got an instance of metakey which should be meta_key.

  • thanks, that wasn't the issue overall though :( Mar 17, 2015 at 0:03

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.