Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Slider with multiple queries WordPress

  • SOLVED

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.

Answers (2)

2010-05-14

Utkarsh Kukreti answers:

<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>


Dan Davies comments:

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.


Utkarsh Kukreti comments:

Try my updated answer.


Dan Davies comments:

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


Utkarsh Kukreti comments:

Could you explain 'related content'?


Dan Davies comments:

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.


Utkarsh Kukreti comments:

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


Dan Davies comments:

Ah yes - quote_reader, not reader_id.

2010-05-14

Oleg Butuzov answers:


<?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>


Oleg Butuzov comments:

<?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?


Oleg Butuzov comments:

sorry there a typo , wait a second...


Oleg Butuzov comments:

update in main post.


Oleg Butuzov comments:

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.


Oleg Butuzov comments:

this is a correct answer on your question


Dan Davies comments:

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


Oleg Butuzov comments:

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).")";
}

?>



Oleg Butuzov comments:

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" />


Oleg Butuzov comments:

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

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


Oleg Butuzov comments:

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