0

I have a single page where I display my current post. Within the post loop, I wanna display another loop inside that shows 3 other related posts by title that share the same text as the current post from the field name "category".

Here's a sample of the code that I have as of now. I got the loop within the loop to work, but it's still not showing the related posts.

<!-- View Work -->
<section class="work-single">
<?php if (have_posts()) : ?> 
<?php while (have_posts()) : the_post(); ?>

  <div class="row">
    <div class="medium-12 columns">
      <!-- Work Title -->
      <div class="intro">
        <h1 class="main-title"><?php echo get_the_title(); ?></h1>
      </div>
    </div>
  </div>

  <div class="work-show">
    <div class="row">

      <!-- Slide Show Image -->
      <div class="medium-12 columns">
        <img src="<?php the_field('large_image'); ?>" alt=""> 
      </div>

      <!-- Description -->
      <div class="medium-8 columns">
        <?php the_field('description'); ?>
      </div>

      <!-- Information -->
      <div class="medium-4 columns">

        <ul class="side-info">
          <li class="client"><h4><?php the_field('client'); ?></h4></li>
          <li class="date"><?php the_field('date'); ?></li>
          <li class="category"><?php the_field('category'); ?></li>
          <li class="leader"><?php the_field('leader'); ?></li>

        </ul>

        <!-- Related Posts Loop -->

        <ul>
          <li>SIMILAR CASE STUDIES</li>
          <?php 
            // args
            $args = array(
              'post_type'   => 'case-studies',
              'posts_per_page'  => 3,
              'meta_key'    => 'category',
              'orderby'   => 'meta_value',
              'order'     => 'rand'
            );

            $theposts = get_posts( $args );
            foreach($theposts as $post) :
            setup_postdata($post);
            ?>
            <li>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            </li>
            <?php
            endforeach;
            wp_reset_postdata();
          ?>
        </ul>

      </div>

    </div>
  </div>

<?php endwhile; else: ?>
  <p>Sorry, no posts matched your criteria.</p>
<?php endif; ?>

1 Answer 1

1

You don't have a meta_value in your query.

From your code I can't tell what you're doing with the meta values but you should be able to do this:

// Store this BEFORE you go into the second loop.
$category = get_field('category');

// The arguments for the second loop
'post_type'   => 'case-studies',
'showposts'  => 3,
'meta_key'    => 'category',
'meta_value'   => $category,
'orderby'     => 'rand'

So what you're doing there is matching the value of the category meta key.

Ideally you would be using a taxonomy to do the above - that's what they are there for.

http://codex.wordpress.org/Function_Reference/register_taxonomy

1
  • Thanks Steven for the answer as that seems to work! Yeah I realize taxonomy is the way to go. But the base structure for categories was ruining everything. Let's say I go that route, how would I show posts related to taxonomy "categories"? ]
    – Xerxes
    Sep 19, 2014 at 17:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.