Get a row from a separate table by matching a posts meta_key to a tables ID column

I am trying to match a posts meta_key value to a separate tables column ID and call that row/array in the single-listing.php.

I have a meta_key value that is called dot_number. The table is called wp_csadata and has a column called dot_number.

Lets say the meta_key equals 12. I need to call that wp_csadata table and match the meta_key value of 12 to the column called dot_number, which has the same number, and return values in the row.

Thanks.

Topic get-row post-meta wpdb Wordpress

Category Web


global $wpdb;

/* grab meta value, it is assumed you know the post id ($id) in question */
$dot_number = get_post_meta( $id, 'dot_number', true );

/* grab all rows with matching value in 'dot_number' column */
$matches_query = $wpdb->get_results(
    "SELECT * FROM " . $wpdb->prefix . "csadata " .
    "WHERE dot_number = " . $dot_number, ARRAY_A
);

// do something with query result, usually iterate over it and display something

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.