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

Limit a query to just 4 Posts, not very usual Query!!!! WordPress

Hello everybody! I have this query which just shows posts which are in the same taxonomy as the posts which is just shown!!! (so this Code definitely belongs on a single.php-Page). It's working super nice and perfect but just one thing is messed up. The limitation with "showposts" just works FOR EACH TERM OF A TAXONOMY not for ALL POSTS SHOWN here in general...but I want to LIMIT it to 4 Posts shown, no matter how many posts would belong for each term of the same taxonomy. Hope you understand me. Please help me I'm really bad in PHP...sorry!
<?php
global $post;
$terms = get_the_terms( $post->ID , 'workcap', 'string');
$do_not_duplicate[] = $post->ID;

if(!empty($terms)){
foreach ($terms as $term) {
query_posts( array(
'workcap' => $term->slug,
'showposts' => 4,
'caller_get_posts' => 1,
'post__not_in' => $do_not_duplicate ) );
if(have_posts()){
while ( have_posts() ) : the_post(); $do_not_duplicate[] = $post->ID; ?>

<div id="twentyleft">
<div id="post-<?php the_ID(); ?>" class="related_work_post">

<div>" title="<?php the_title() ?>" class="linked"><?php $image = get_post_meta($post->ID, "Image1", true); echo $image; ?></div>
<h3>" title="<?php the_title() ?>" class="linked"><?php the_title() ?></h3>
<?php $subtitle = get_post_meta($post->ID, "Subtitle", true); echo $subtitle; ?>

</div>
</div><!-- twentyleft END -->
<?php endwhile; wp_reset_query();
}}}
?>

Answers (4)

2011-03-29

Andrzej answers:

Hi there,

Try using:

query_posts( array(
'workcap' => $term->slug,
'numberposts' => 4,
'caller_get_posts' => 1,
'post__not_in' => $do_not_duplicate ) );


...so 'numberposts' instrad of 'showposts'

2011-03-29

Jonah Schulte answers:

Try using

'posts_per_page' => 4,

instead of

'showposts' => 4,

2011-03-29

Nilesh shiragave answers:

If you are using wordpress 3.1 use following code

<?php

global $post;

$terms = get_the_terms( $post->ID , 'workcap', 'string');

$do_not_duplicate[] = $post->ID;



if(!empty($terms)){

foreach ($terms as $term) {

query_posts( array(

'workcap' => $term->slug,

'posts_per_page' => 4,

'ignore_sticky_posts' => 1,

'post__not_in' => $do_not_duplicate ) );

if(have_posts()){

while ( have_posts() ) : the_post(); $do_not_duplicate[] = $post->ID; ?>



<div id="twentyleft">

<div id="post-<?php the_ID(); ?>" class="related_work_post">



<div>" title="<?php the_title() ?>" class="linked"><?php $image = get_post_meta($post->ID, "Image1", true); echo $image; ?></div>

<h3>" title="<?php the_title() ?>" class="linked"><?php the_title() ?></h3>

<?php $subtitle = get_post_meta($post->ID, "Subtitle", true); echo $subtitle; ?>



</div>

</div><!-- twentyleft END -->

<?php endwhile; wp_reset_query();

}}}

?>



Goermez Wuff comments:

Acutally all of the three Ideas do NOT work. I've already tried to change "showposts" to "posts_per_page" and "numberposts" before, and I tested it now again! It just doesn't make any difference.

And the code from Nilseh just adds the "ignore_sticky_posts' => 1,"-Part which even makes no difference at all. Very poor...

I hope that somebody can help me!!!!


Nilesh shiragave comments:

If the wordpress "post_per_page","showposts","numberposts" not working then try PHP to break while loop using count variable.

<?php

global $post;
$number_of_posts=4; // Change it with your value
$terms = get_the_terms( $post->ID , 'workcap', 'string');

$do_not_duplicate[] = $post->ID;



if(!empty($terms)){

foreach ($terms as $term) {

query_posts( array(

'workcap' => $term->slug,

'showposts' => 4,

'caller_get_posts' => 1,

'post__not_in' => $do_not_duplicate ) );

if(have_posts()){
$count=0;
while ( have_posts() ) : the_post(); $do_not_duplicate[] = $post->ID; ?>

<?php
/* Code To Break While loop if it exceeds the limit */
$count++;
if($count==$number_of_posts)
{
break;
}
/* Ends */
?>



<div id="twentyleft">

<div id="post-<?php the_ID(); ?>" class="related_work_post">



<div><a title="<?php the_title() ?>" class="linked"><?php $image = get_post_meta($post->ID, "Image1", true); echo $image; ?></a></div>

<h3><a title="<?php the_title() ?>" class="linked"><?php the_title() ?></a></h3>

<?php $subtitle = get_post_meta($post->ID, "Subtitle", true); echo $subtitle; ?>



</div>

</div><!-- twentyleft END -->

<?php endwhile; wp_reset_query();

}}}

?>


Goermez Wuff comments:

That Idea with the Break is super cool! But it does NOT work. I think this is not working, because this query is showing posts of every Term of the Taxonomy and the break also just works for every Term of the Taxonomy...so it does not breaking the whole thing after showing 4 Posts as a whole. Maybe it's all because of the "FOREACH" thing there...isn't it? I mean I do not understand it exactly...hope someone got more ideas.


Nilesh shiragave comments:

so you want total 4 posts from that taxonomy?


Goermez Wuff comments:

Yeoosnsn.... I have posts. I have my custom taxonomy "workcap". The Custom Taxonomy "workcap" got a lot of Terms in it. To my posts I alway add some of those terms of the Custom Taxonomy "workcap" to them. And now I want some RANDOM Posts from different terms which belong also to the just shown Post. The MAXIMUM Number of Related Posts shown should be 4!


Nilesh shiragave comments:

can you try this code instead of your code..

<?php
global $post;
$category = get_the_terms( $post->ID , 'workcap', 'string');
$myposts = get_posts(array('numberposts' => 4, 'offset' => 0, 'category__in' => $category, 'post__not_in' => array($post->ID),'post_status'=>'publish','orderby' => 'rand','order' => 'ASC'));
foreach($myposts as $post) :
setup_postdata($post);
?>
<div id="twentyleft">

<div id="post-<?php the_ID(); ?>" class="related_work_post">



<div><a title="<?php the_title() ?>" class="linked"><?php $image = get_post_meta($post->ID, "Image1", true); echo $image; ?></a></div>

<h3><a title="<?php the_title() ?>" class="linked"><?php the_title() ?></a></h3>

<?php $subtitle = get_post_meta($post->ID, "Subtitle", true); echo $subtitle; ?>

</div>

</div><!-- twentyleft END -->
<?php endforeach; ?>
<?php wp_reset_query(); ?>


Goermez Wuff comments:

This one just brings out an Error:

Catchable fatal error: Object of class stdClass could not be converted to string in /is/htdocs/wp1124961_NBRRTX73JS/www/joeybottle/wp-includes/query.php on line 1708

I don't have an idea why this is and what it means.

2011-03-30

Denzel Chia answers:

Hi,

I am not very sure If I understand your question. But I assume that you code works and you want to limit the output to only 4 posts.

I think can you do it by using a counter.


<?php

$count = 0;

global $post;

$terms = get_the_terms( $post->ID , 'workcap', 'string');

$do_not_duplicate[] = $post->ID;

if(!empty($terms)){

foreach ($terms as $term) {

query_posts( array(

'workcap' => $term->slug,

'showposts' => 4,

'caller_get_posts' => 1,

'post__not_in' => $do_not_duplicate ) );

if(have_posts()){

while ( have_posts() ) : the_post(); $do_not_duplicate[] = $post->ID; ?>

<?php if($count<4): ?>

<div id="twentyleft">

<div id="post-<?php the_ID(); ?>" class="related_work_post">


<div>" title="<?php the_title() ?>" class="linked"><?php $image = get_post_meta($post->ID, "Image1", true); echo $image; ?></div>

<h3>" title="<?php the_title() ?>" class="linked"><?php the_title() ?></h3>

<?php $subtitle = get_post_meta($post->ID, "Subtitle", true); echo $subtitle; ?>


</div>

</div><!-- twentyleft END -->

<?php $count++; endif; ?>

<?php endwhile; wp_reset_query();

}}}

?>


Goermez Wuff comments:

Yes this one is pretty cool! But there is still one problem though :( If I have a post with maybe 4 or 5 Terms of a Taxonomy than I want to Query EXACTLY 4 Random Posts which belong to thos 4 or 5 Terms of that Taxonomy.

With your code now there will show up 4 Posts which belong to the First Term (because there exist more than 4 Posts which also have the First Term in it)...you understand the Problem???

So maybe it's to complex what I want...I think it's super hard but maybe someone understands me and knows the solution...


Denzel Chia comments:

Please see if this works, if not, I no more ideas.
This idea is to collect the post ids from the query and randomly select 4 ids for the next query, before showing the results. explanation in comments.

Hope these code does not throw errors. I did not test them.


<?php
//declare blank result container for collecting post ids,
$result_container = array();

//start query to collect post ids.
global $post;

$terms = get_the_terms( $post->ID , 'workcap', 'string');

$do_not_duplicate[] = $post->ID;

if(!empty($terms)){

foreach ($terms as $term) {

query_posts( array(

'workcap' => $term->slug,

'showposts' => 4,

'caller_get_posts' => 1,

'post__not_in' => $do_not_duplicate ) );

if(have_posts()){

while ( have_posts() ) : the_post(); $do_not_duplicate[] = $post->ID; ?>

<?php
//push all posts id into $result_container for next query.
global $post;
$post_id = $post->ID;
array_push($result_container,$post_id);
?>

<?php endwhile; wp_reset_query();

}}}
?>
<?php
//randomly select 4 ids from $result_container for next query.
$random_post_ids = array_rand($result_container,4);
?>

<?php query_posts( array( 'post__in' => $random_post_ids ) ); ?>

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

<div id="twentyleft">

<div id="post-<?php the_ID(); ?>" class="related_work_post">

<div>" title="<?php the_title() ?>" class="linked"><?php $image = get_post_meta($post->ID, "Image1", true); echo $image; ?></div>

<h3>" title="<?php the_title() ?>" class="linked"><?php the_title() ?></h3>

<?php $subtitle = get_post_meta($post->ID, "Subtitle", true); echo $subtitle; ?>

</div>

</div><!-- twentyleft END -->

<?php endwhile; endif; ?>



Thanks.
Denzel


Denzel Chia comments:

Sorry, forgot to declare an array.
Please use this instead.


<?php

//declare blank result container for collecting post ids,

$result_container = array();

//start query to collect post ids.

global $post;

$terms = get_the_terms( $post->ID , 'workcap', 'string');

$do_not_duplicate[] = $post->ID;

if(!empty($terms)){

foreach ($terms as $term) {

query_posts( array(

'workcap' => $term->slug,

'showposts' => 4,

'caller_get_posts' => 1,

'post__not_in' => $do_not_duplicate ) );

if(have_posts()){

while ( have_posts() ) : the_post(); $do_not_duplicate[] = $post->ID; ?>

<?php

//push all posts id into $result_container for next query.

global $post;

$post_id = $post->ID;

array_push($result_container,$post_id);

?>

<?php endwhile; wp_reset_query();

}}}

?>

<?php
//randomly select 4 ids from $result_container for next query.
$random_post_ids = array();
$random_post_ids = array_rand($result_container,4);
?>

<?php query_posts( array( 'post__in' => $random_post_ids ) ); ?>

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

<div id="twentyleft">

<div id="post-<?php the_ID(); ?>" class="related_work_post">

<div>" title="<?php the_title() ?>" class="linked"><?php $image = get_post_meta($post->ID, "Image1", true); echo $image; ?></div>

<h3>" title="<?php the_title() ?>" class="linked"><?php the_title() ?></h3>

<?php $subtitle = get_post_meta($post->ID, "Subtitle", true); echo $subtitle; ?>

</div>

</div><!-- twentyleft END -->

<?php endwhile; endif; ?>


Thanks.