0
votes

I am working on my first Wordpress child theme, for an existing theme.

The wp_postmeta table has the following columns:

meta_id
post_id
meta_key
meta_value

A few meta keys of interest to me:

book_title
exhibition_name

I would like to get all the book titles, and their corresponding post IDs, that match a specific exhibition name.

If I use the get_post_meta call, the parameter $post_id is required, so it's a dead end (as far as I know).

Any idea how I can accomplish what I would like, using the WordPress API?

Solution

The answer was provided by @Junaid but the syntax had a minor error in it:

$args = array(
    'post_type' => 'post',
    'meta_query' => array(
        array(
            'key' => 'exhibition_name',
            'value' => 'Science Exhibition'
        )
    )
);

$query = new WP_Query($args);

if ($query->have_posts()) {
    while ($query->have_posts()) {
        $query->the_post();
        $post_id = get_the_ID();
        $title = get_post_meta($post_id, 'book_title');
    }
}
1

1 Answers

1
votes

Have you tried using WP_Query and pass meta_query arguments? Try this code

$args = array(
    'post_type' => 'post',
    'meta_query' => array(
        'key' => 'exhibition_name',
        'value' => 'Science Exhibition'
    )
);

$query = new WP_Query($args);

if ($query->have_posts()) :
    while ($query->have_posts()) :
        $query->the_post();
        $post_id = get_the_ID();
        $title = get_post_meta($post_id, 'book_title');
    endwhile;
endif;

NOT TESTED