WordPress.org

Ready to get started?Download WordPress

Forums

Show array of post ID's (2 posts)

  1. Mr Press
    Member
    Posted 2 years ago #

    In the code below $idspost [] = $post->ID; stores an array of post ID's belonging to posts related to the current post by tag. How can I display those posts, only if they exist? I need to pass on the array $idspost [] = $post->ID; to something like a standard loop:

    <?php if (have_posts()) : ?>
      <div><p>some header text and styling</p> <!--if no posts nothing will be shown-->
        <?php while (have_posts()) : the_post(); ?>
          do stuff... <!--get the permalink, title etc...-->
        <?php endwhile; ?>
      </div>
    <?php endif; ?>

    My code:

    <?php
    $tags = wp_get_post_tags($post->ID);
    if ($tags) {
      $second_tag = $tags[1]->term_id;
      $args=array(
        'tag__in' => array($second_tag),
        'post__not_in' => array($post->ID),
        'showposts'=>5, //Display this number of related posts
        'ignore_sticky_posts'=>1
       );
      $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post();
      if (!in_array($post->ID, $ids)) {; $idspost [] = $post->ID; ?>
    
    <?php }
        $ids[]= $post->ID;
        endwhile;
      }
    }
    ?>
  2. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    try and use

    'post__in' => $idspost

    in a new query or in query_posts() before your standard loop.

    http://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags