In WordPress, i am using a plugin named "Business directory" that plugin created a table, name "wp_wpbdp_listing_fees". and there have some column.

column name "listing_id" has all "custom post type" post id and column name "fee_id" has two different values [2 and 4] The problem is, i want to show all post title what matched with "fee_id" = 4

custom_post_type name = wpbdp_listing

I hope this image will make it more clear :)


Waiting for an effective solution :)

Thanks in advance


The following should do the trick:

global $wpdb ;

// get the post_ids from the custom table
$sql = "SELECT listing_id FROM {$wpdb->prefix}wpdbdp_listing_fees WHERE fee_id = %d" ;
$sql = $wpdb->prepare ($sql, 4) ;
$post_ids = $wpdb->get_col ($sql) ;

// get the custom posts from the posts table
$args = array (
    'post__in' => $post_ids,
    'post_type' => 'wpbdp_listing',
    ) ;
$posts = new WP_Query ($args) ;

foreach ($posts->posts as $the_post)
    echo $the_post->post_title ;

Note the use of $wpdb->prefix in the SQL statement.

  • Is there any way to echo isting_id beside post title? – rasel mahmud Feb 26 '17 at 11:04
  • sure: since the listing_id is the post ID, just echo $the_post->ID – Paul 'Sparrow Hawk' Biron Feb 26 '17 at 15:19

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.