logo

$10
Slider with multiple queries

I've got a vertical featured content slider. On the links side I want to run a query that gets the title (which is a number), the excerpt, the thumbnail and a custom field. On the content side, I'm running a query based on the number in the titles of the links section. This will be easier to understand if you look at the code:

<div id="featured-area">
<div id="s1" class="pics">
<?php $q1= new WP_Query('cat=4&showposts=3'); ?>
<?php if($q1->have_posts()) : ?><?php $counter = 1; while($q1->have_posts()) : $q1->the_post(); ?>
<?php $readerid = the_title('','',false); ?>
<div>
<div class="anythingSlider">
<div class="wrapper">
<ul id="feature-slides">
<?php $q2 = new WP_Query('showposts=5&orderby=rand&cat=5&meta_key=quote_reader&meta_value='. $readerid.''); ?>
<?php if($q2->have_posts()) : ?><?php while($q2->have_posts()) : $q2->the_post(); ?>
<li><?php the_content(); ?></li>
<?php endwhile; endif; ?>
</ul>
</div>
</div>
</div>
<?php wp_reset_query(); ?>
<?php endwhile; endif; ?>
</div>

<div id="slider-control">
<?php $q3= new WP_Query('cat=4&showposts=3'); ?>
<?php if($q3->have_posts()) : ?><?php $counter = 1; while($q3->have_posts()) : $q3->the_post(); ?>
<?php $thumbid = get_post_thumbnail_id($post->ID); ?>
<?php if($counter == 1) { $class = 'active'; } else { $class = 'notactive'; } ?>
<div class="featitem <?= $class; ?>">
<a href="<?php the_permalink(); ?>"><img src="<?php bloginfo('template_directory'); ?>/timthumb.php?src=<?php echo wp_get_attachment_url($thumbid); ?>&h=120&w=120&zc=1" alt="<?php the_title(); ?>" width="120" height="120" /></a>
<h3><a href="<?php the_permalink(); ?>"><?php echo get_post_meta($post->ID, "reader_name", $single = true); ?><br /><span>ID <?php the_title(); ?></span></a></h3>
<p><?php echo excerpt(19); ?></p>
<span class="order"><?php echo $counter; ?></span>
</div>
<?php $counter++; endwhile; endif; ?>
</div>
<div class="cboth"></div>
</div>


This works fine - but I need to somehow merge these queries, as I'd like to use orderby=rand in my original query, but the other queries still need to relate to the original content.

====Edit====

Basically, what I need to do is add orderby=rand to the $q1 query. I then need $q3 to duplicate $q1 - so $q1 and $q3 need to return the same random posts.

Dan Davies | 05/14/10 at 2:04pm | Edit


(2) Possible Answers Submitted...

  • avatar
    Last edited:
    06/20/10
    7:29pm
    Utkarsh Kukreti says:

    <div id="featured-area">
    <?php $print_later = ''; ?>
    <div id="s1" class="pics">
    <?php $q1= new WP_Query('cat=4&showposts=3'); ?>
    <?php if($q1->have_posts()) : ?><?php $counter = 1; while($q1->have_posts()) : $q1->the_post(); ?>
    <?php $readerid = the_title('','',false); ?>
    <div>
    <div class="anythingSlider">
    <div class="wrapper">
    <ul id="feature-slides">
    <?php $q2 = new WP_Query('showposts=5&orderby=rand&cat=5&meta_key=quote_reader&meta_value='. $readerid.''); ?>
    <?php if($q2->have_posts()) : ?><?php while($q2->have_posts()) : $q2->the_post(); ?>
    <li><?php the_content(); ?></li>
    <?php endwhile; endif; ?>
    </ul>
    </div>
    </div>
    </div>

    <?php $q1->the_post(); ?>
    <?php ob_start(); ?>

    <?php $thumbid = get_post_thumbnail_id($post->ID); ?>
    <?php if($counter == 1) { $class = 'active'; } else { $class = 'notactive'; } ?>
    <div class="featitem <? echo $class; ?>">
    <a href="<?php the_permalink(); ?>"><img src="<?php bloginfo('template_directory'); ?>/timthumb.php?src=<?php echo wp_get_attachment_url($thumbid); ?>&h=120&w=120&zc=1" alt="<?php the_title(); ?>" width="120" height="120" /></a>
    <h3><a href="<?php the_permalink(); ?>"><?php echo get_post_meta($post->ID, "reader_name", $single = true); ?><br /><span>ID <?php the_title(); ?></span></a></h3>
    <p><?php echo excerpt(19); ?></p>
    <span class="order"><?php echo $counter; ?></span>
    </div>
    <?php $counter++; ?>

    <?php $print_later .= ob_get_clean(); ?>

    <?php wp_reset_query(); ?>
    <?php endwhile; endif; ?>
    </div>

    <div id="slider-control">
    <?php echo $print_later; ?>
    </div>
    <div class="cboth"></div>
    </div>

    Previous versions of this answer: 05/14/10 at 2:24pm

    • 05/14/10 2:18pm

      Dan Davies says:

      Unfortunately this didn't display the correct content.

      $q1 will have orderby=rand
      $q2 queries posts from a testimonials category with posts that have the custom field value that's the same as the_title of the posts in $q1
      $q3 re-runs $q1 to get additional content in the other part of the jQuery content slider.

    • 05/14/10 2:25pm

      Utkarsh Kukreti says:

      Try my updated answer.

    • 05/14/10 2:28pm

      Dan Davies says:

      I added orderby=rand to $q1 and unfortunately $q2 doesn't query the related content.

    • 05/14/10 2:32pm

      Utkarsh Kukreti says:

      Could you explain 'related content'?

    • 05/14/10 2:45pm

      Dan Davies says:

      The titles of the posts in cat 4 ($q1) are a number (0001, 0002, 0003, etc.) - this number is the ID of a user on a different system.

      The posts in cat 5 ($q2) are testimonials. They have a custom field (reader_id) that has a value of 0001/0002 (the ID of a user on a different system).

      $q1 needs to query 3 random posts from category 4, and it stores the_title in a variable ($readerid).

      $q2 needs to query posts from category 5 with the custom field key of reader_id and the value of $readerid.

      $q3 needs to re-run $q1 because the content is being displayed in a jQuery slider - $q1 populates the links to each slide and $q3 populates the content of each slide. However, if $q1 is orderby=rand, I don't know how to get $q3 to query the same posts.

    • 05/14/10 2:50pm

      Utkarsh Kukreti says:

      You said key = reader_id, but your declaration of $q2 says key = quote_reader.

    • 05/14/10 3:19pm

      Dan Davies says:

      Ah yes - quote_reader, not reader_id.

  • avatar
    Last edited:
    06/20/10
    7:29pm
    Oleg Butuzov says:


    <?php
    function returnrand(){
    return ' RAND() ';
    }

    function returnidorder(){

    global $QueriedPosts, $wpdb;

    return " field(".$wpdb->posts.".ID, ".implode(",", $QueriedPosts).")";

    }

    ?>


    <div id="featured-area">

    <div id="s1" class="pics">
    <?php
    add_filter('posts_orderby_request', 'returnrand');
    global $QueriedPosts;
    ?>
    <?php $q1= new WP_Query('cat=4&showposts=3'); ?>

    <?php remove_filter('posts_orderby_request', 'returnrand'); ?>
    <?php if($q1->have_posts()) : ?><?php $counter = 1; while($q1->have_posts()) : $q1->the_post(); ?>
    <?php
    $QueriedPosts[] = $post->ID;
    ?>

    <?php $readerid = the_title('','',false); ?>

    <div>

    <div class="anythingSlider">

    <div class="wrapper">

    <ul id="feature-slides">

    <?php $q2 = new WP_Query('showposts=5&orderby=rand&cat=5&meta_key=quote_reader&meta_value='. $readerid.''); ?>

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

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

    <?php endwhile; endif; ?>

    </ul>

    </div>

    </div>

    </div>

    <?php wp_reset_query(); ?>

    <?php endwhile; endif; ?>

    </div>



    <div id="slider-control">
    <?php add_filter('posts_orderby_request', 'returnidorder'); ?>
    <?php $q3= new WP_Query(array('post__in'=>$QueriedPosts, 'cat' => 4, 'showposts' => 3)); ?>
    <?php remove_filter('posts_orderby_request', 'returnidorder'); ?>
    <?php if($q3->have_posts()) : ?><?php $counter = 1; while($q3->have_posts()) : $q3->the_post(); ?>


    <?php $thumbid = get_post_thumbnail_id($post->ID); ?>

    <?php if($counter == 1) { $class = 'active'; } else { $class = 'notactive'; } ?>

    <div class="featitem <?= $class; ?>">

    <a href="<?php the_permalink(); ?>"><img src="<?php bloginfo('template_directory'); ?>/timthumb.php?src=<?php echo wp_get_attachment_url($thumbid); ?>&h=120&w=120&zc=1" alt="<?php the_title(); ?>" width="120" height="120" /></a>

    <h3><a href="<?php the_permalink(); ?>"><?php echo get_post_meta($post->ID, "reader_name", $single = true); ?><br /><span>ID <?php the_title(); ?></span></a></h3>

    <p><?php echo excerpt(19); ?></p>

    <span class="order"><?php echo $counter; ?></span>

    </div>

    <?php $counter++; endwhile; endif; ?>

    </div>

    <div class="cboth"></div>

    </div>

    Previous versions of this answer: 05/14/10 at 2:21pm | 05/14/10 at 2:22pm | 05/14/10 at 2:23pm | 05/14/10 at 2:31pm | 05/14/10 at 2:40pm | 05/14/10 at 2:40pm | 05/14/10 at 2:50pm | 05/14/10 at 2:51pm | 05/14/10 at 3:03pm | 05/14/10 at 3:06pm | 05/14/10 at 3:07pm | 05/14/10 at 3:07pm | 05/14/10 at 3:15pm | 05/14/10 at 3:17pm

    • 05/14/10 2:46pm

      Oleg Butuzov says:

      <?php
      function returnrand(){
      return ' RAND() ';
      }

      ?>


      <div id="featured-area">

      <div id="s1" class="pics">
      <?php add_filter('posts_orderby_request', 'returnrand); ?>
      <?php $q1= new WP_Query('cat=4&showposts=3'); ?>

      <?php remove_filter('posts_orderby_request', 'returnrand'); ?>
      <?php if($q1->have_posts()) : ?><?php $counter = 1; while($q1->have_posts()) : $q1->the_post(); ?>
      <?php
      $QueriedPosts[] = $post->ID;
      ?>

      <?php $readerid = the_title('','',false); ?>

      <div>

      <div class="anythingSlider">

      <div class="wrapper">

      <ul id="feature-slides">

      <?php $q2 = new WP_Query('showposts=5&orderby=rand&cat=5&meta_key=quote_reader&meta_value='. $readerid.''); ?>

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

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

      <?php endwhile; endif; ?>

      </ul>

      </div>

      </div>

      </div>

      <?php wp_reset_query(); ?>

      <?php endwhile; endif; ?>

      </div>



      <div id="slider-control">

      <?php $q3= new WP_Query(array('post__in'=>$QueriedPosts, 'cat' => 3, 'showposts' => 3)); ?>

      <?php if($q3->have_posts()) : ?><?php $counter = 1; while($q3->have_posts()) : $q3->the_post(); ?>

      <?php $thumbid = get_post_thumbnail_id($post->ID); ?>

      <?php if($counter == 1) { $class = 'active'; } else { $class = 'notactive'; } ?>

      <div class="featitem <?= $class; ?>">

      <a href="<?php the_permalink(); ?>"><img src="<?php bloginfo('template_directory'); ?>/timthumb.php?src=<?php echo wp_get_attachment_url($thumbid); ?>&h=120&w=120&zc=1" alt="<?php the_title(); ?>" width="120" height="120" /></a>

      <h3><a href="<?php the_permalink(); ?>"><?php echo get_post_meta($post->ID, "reader_name", $single = true); ?><br /><span>ID <?php the_title(); ?></span></a></h3>

      <p><?php echo excerpt(19); ?></p>

      <span class="order"><?php echo $counter; ?></span>

      </div>

      <?php $counter++; endwhile; endif; ?>

      </div>

      <div class="cboth"></div>

      </div>


      this?

    • 05/14/10 2:47pm

      Oleg Butuzov says:

      sorry there a typo , wait a second...

    • 05/14/10 2:51pm

      Oleg Butuzov says:

      update in main post.

    • 05/14/10 3:04pm

      Oleg Butuzov says:

      Dan take a look to my post please. main post.

      i am using two filter functions.

      1) is giving order by rand

      after that i am saving the id's of the posts.

      2) filter for orderby rule is providing sql to set order by ids.

    • 05/14/10 3:10pm

      Oleg Butuzov says:

      this is a correct answer on your question

    • 05/14/10 3:22pm

      Dan Davies says:

      Hrm - this fails to return content from $q1 and $q3, but I'll see if I can modify it to work.

    • 05/14/10 3:23pm

      Oleg Butuzov says:

      you code from first post. with two filter functions.


      <?php
      function returnrand(){
      return ' RAND() ';
      }


      function returnidorder(){
      global $QueriedPosts, $wpdb;
      return " field(".$wpdb->posts.".ID, ".implode(",", $QueriedPosts).")";
      }

      ?>


    • 05/14/10 3:27pm

      Oleg Butuzov says:

      local test proof, i have just modified inner work to output posts titles

      hr and sql is from q3

      <img alt="Image #876921, 79 KB" src="http://img.leprosorium.com/876921" title="Image #876921, 79 KB" />

    • 05/15/10 2:04pm

      Oleg Butuzov says:

      filters for order by dosn't help you? strange.

      1) have give you rand
      2) give an order by prevoius saved ids.

    • 05/15/10 2:05pm

      Oleg Butuzov says:

      can you provide you current code with two filters described in my first post. so i can see is there evrything ok?

This question has expired.





Current status of this question: Completed