5

Please look at the picture below. I want to get post_id from meta_value = 93. How can I do that?

4
  • Please see stackoverflow.com/questions/4116419/…
    – WebElaine
    Mar 14 '17 at 16:49
  • I have do this and it's working now : global $wpdb; $sql = $wpdb->prepare("SELECT post_id FROM " . $wpdb->prefix . "postmeta WHERE meta_key = '_wbs_crosssells' && meta_value like '%%%s%%'",serialize($product_id)); $res = $wpdb->get_results( $sql, OBJECT ); return $res;
    – huykon225
    Mar 14 '17 at 16:55
  • 1
    Just keep in mind it's not a recommended method and queries may run slowly, potentially causing site stability issues. Glad you found a solution though.
    – WebElaine
    Mar 14 '17 at 16:56
  • Use meta API, it's cached and optimized $meta = get_post_meta(96, '_wbs_crosssells', true);
    – Ismail
    Mar 14 '17 at 21:24
6

Try this. It will work.

$prepare_guery = $wpdb->prepare( "SELECT post_id FROM $wpdp->posts where meta_key ='_Wps_crossells' and meta_value like '%%d%'", $meta_value );
$get_values = $wpdb->get_col( $prepare_guery );

Let me know it's working or not.

3
  • yes . It's working now, same my comment above
    – huykon225
    Mar 15 '17 at 1:13
  • okk. then fine.
    – Thilak
    Mar 15 '17 at 3:48
  • 2
    You mean FROM $wpdb->postmeta WHERE.
    – nmr
    Mar 27 '19 at 8:49
3
global $wpdb;

$tbl = $wpdb->prefix.'postmeta';

$prepare_guery = $wpdb->prepare( "SELECT post_id FROM $tbl where meta_key ='_Wps_crossells' and meta_value like '%%d%'", $meta_value );

$get_values = $wpdb->get_col( $prepare_guery );

Note: Table Should be $wpdb->prefix.'postmeta';

1
  • It should be a comment on another answer. When you get 50 reputation points you will be able to comment on any posts.
    – nmr
    Mar 27 '19 at 8:53

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.