1

I have 6 related posts by tag in every post. But some tags have only 2 or 3 posts. So in these posts, related posts section shows only 2 or 3 post.

I want to complete related posts (if not 6) with the new posts.

I tried many codes but if tags have 2 posts, than related posts show 2 post only. Can I add new posts to complete to 6?

Or can I display recent post order by same tag? So, there will be 6 posts and same tags will be shown first.

Thanks in advance!

2
  • You can try it. Here! Good luck for you :D Commented Feb 1, 2018 at 9:23
  • Thank you so much. I tried that but it shows only recent posts. It does not show posts with the same tag. I could not figure out what is the $brand_name.
    – ahmet
    Commented Feb 1, 2018 at 9:40

2 Answers 2

1

For a list, the code would look something like this:

<ul>
<?php $args = array(
  'tag' => 'travel', 
  'posts_per_page' => 3); ?>

  <?php $recent_posts = new WP_Query( $args ); ?>

  <?php if ($recent_posts->have_posts()) : 
        while ($recent_posts->have_posts()) : $recent_posts->the_post();
   ?>


    <li><?php the_title(); ?></li>

  <?php endwhile; 

     else :
     echo "Nothing";
      endif;
      wp_reset_postdata();
  ?>
</ul>
0
<div class="page-loop">

    <?php
      while (have_posts()) : the_post();
        $page_title = strtolower(get_the_title());
        the_title('<h1>','</h1>');
      ?>
        <p><?php the_content(); ?><p>
    <?php endwhile;?>

</div>

<!-- Get the most recent post that has been tagged with the page title -->
<div class="related-posts">

    <?php
      $args = array(
        'tag' => $page_title,
        'posts_per_page' => 1,
      );
      $query = new WP_Query($args);
      if ($query->have_posts()) :
        while ($query->have_posts()) : $query->the_post();
          the_title('<h1>','</h1>');
        ?>
        <p><?php the_content(); ?><p>
    <?php endwhile; else: ?>
      <p>Sorry, no posts with this tag!</p>
    <?php endif; wp_reset_query(); ?>

</div>




/** register the meta box */

function my_theme_add_meta_boxes() { global $post; add_meta_box( 'my-theme-meta', 'Choose a tag', 'my_theme_print_page_meta', 'page', 'normal', 'high' ); } add_action('add_meta_boxes', 'my_theme_add_meta_boxes');

/** Add extra meta to the page */
function my_theme_print_page_meta() {
    global $post;
    $page_tags = get_post_meta($post->ID, '_page-tags', true);
    ?>
    <label for="page-tags">Add a 'tag'</label>
    <input type="text" class="page-tags" name="page-tags" value="<?php echo esc_attr($page_tags); ?>" />
<?php

} /** Save post meta */ function my_theme_save_custom_meta() { global $post;

// Stops WP from clearing post meta when autosaving
if( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
  return $post->ID;
}
if (isset($_POST['page-tags'])) {
    $clean = sanitize_text_field($_POST['page-tags']);
    update_post_meta($post->ID, '_page-tags', $clean);
}

} add_action('save_post', 'my_theme_save_custom_meta');

<div class="page-loop">

<?php
  while (have_posts()) : the_post();
    $page_tags = get_post_meta($post->ID, '_page-tags', true);
    the_title('<h1>','</h1>');
  ?>
    <p><?php the_content(); ?><p>
<?php endwhile;?>

$page_tags, 'posts_per_page' => 1, ); $query = new WP_Query($args); if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); the_title('

','

'); ?>

Sorry, no posts with this tag!

1
  • Dharmishtha, thanks so much. Is there any way to display new posts even there is no posts with same tag. Let's say I created new post with the tag NewTag. And there is no other posts with this tag. Then related posts section will display -Sorry, no posts with this tag!- Can't we display new posts if empty?
    – ahmet
    Commented Feb 1, 2018 at 10:21

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.