1

I'm trying to get the meta info by key rw_related_link for each post but my code is wrong since instead get the ones related to the current post I got the same in all post and that's wrong. This is what I have at single.php:

if (have_posts()) {
    while (have_posts()) {
        the_post();       

        $normal_args   = array(
            'ignore_sticky_posts' => 1,
            'order'               => 'desc',
            'meta_query'          => array(
                array(
                    'key'     => 'rw_related_link'
                )
            ),
            'post_status'         => 'publish',
            'posts_per_page'      => 6
        );

        $normal_query  = new WP_Query( $normal_args );

        if ($normal_query->have_posts()) { ?>
            <section class="single_relations sih1">
                <ul>
                    <?php while ($normal_query->have_posts()) {
                        $normal_query->the_post();
                        ?>
                        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
                    <?php
                    } ?>
                </ul>
            </section>
        <?php }
        wp_reset_postdata();
    }
}

What is wrong in my code?

2

The way you did in your code you are querying posts with the key rw_related_link.

If I understood you right, you must use get_post_meta inside the loop.

<?php while ($normal_query->have_posts()) {
          $normal_query->the_post(); 
          $related_link = get_post_meta(get_the_ID(), 'rw_related_link', true);
          // The value you want is in $related_link variable.
?>
          <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php } ?>

Hope it helps.

2
  • I change my code a bit and now I get the meta by rw_related_link as follow: $related_link = get_post_meta(get_the_ID(), 'rw_related_link', true) that's fine, that returns an array of values and that values are the ID's of other posts, how do I get the the_title() and the_permalink() for those related posts? – ReynierPM Feb 16 '15 at 23:18
  • 2
    Review the Codex entries for the_title() and the_permalink() - you'll note they make use of two additional functions, get_the_title() and get_permalink(), to which you may pass a post ID in order to retrieve the corresponding information. – bosco Feb 16 '15 at 23:38
1

The arguments passed to the WP_Query class's constructor are used to describe the posts you are "looking for" in, and wish to retrieve from the database - the 'meta_query' argument is used to retrieve posts that contain a particular meta key and/or value.

In order to actually manipulate (i.e. create/retrieve/update/delete) post meta-data, you should be using the Metadata API - in this instance, the get_metadata() function in particular.

1
  • Can you take a look to the comment I leave on @Bruno answer? Perhaps you could help me too – ReynierPM Feb 16 '15 at 23:20

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.