1

Recently I began to use the Custom Fields. I would like to display a listing of posts which contains the meta_key "oldtimer" and also get the corresponding meta_value.

Here is the expected result:

<table>
<tr>
    <td>Title of post 1</td>
    <td>OLD-39283</td>
</tr>
<tr>
    <td>Title of post 2</td>
    <td>OLD-22445</td>
</tr>
<tr>
    <td>Title of post 3</td>
    <td>OLD-32145</td>
</tr></table>

Here is the PHP code I have for now. I don't know how to get the meta_value for each post:

$args =
    array(
    'posts_per_page'         => -1,
    'order'                  => 'ASC',
    'orderby'                => 'title',
    'meta_key'               => 'oldtimer'
);

$the_query = new WP_Query($args);
// The Loop
if ( $the_query->have_posts() ) {
    echo '<table>';
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        echo '<tr><td><a href="'. get_the_permalink() .'">' . get_the_title() . '</td>';
        echo '<td>meta_key</td></tr>';
    }
    echo '</table>';

    /* Restore original Post Data */
    wp_reset_postdata();
    } else {
    // no posts found
}

Here is the information on SQL side: enter image description here

Is anyone has an idea? Any suggestion about this will be much appreciated.

1 Answer 1

0

You can just fetch the post meta manually within the loop as follows:

get_post_meta( get_the_ID(), 'oldtimer', true );

That should get you the correct value. Hope it helps 👍

1
  • It's working as expected. Thank you :)
    – lapulpe
    Sep 5, 2019 at 15:30

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.