• 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();
    }}}
    ?>

Viewing 10 replies - 1 through 10 (of 10 total)
  • I think this what you want:

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

    (@joeybottle)

    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.

    Did you change showposts to 1 instead of 4?

    Thread Starter joeybottle

    (@joeybottle)

    No I didn’t in the first place! But now I did. It seems to work…there are never more than 4 Posts at the time. But the problem in this case now is, when I Tag a Post with just 1 Term of a Taxonomy it will also just show me JUST ONE Related Post..you understand what I mean?? Mhhh

    It would be awesome to still have “showposts” on 4 and also limit the maximal number of all posts together to 4 as well… Mhh

    So tell me if this is what you want:

    If the first term has 5 posts, and the second has 3, you only want to show 4 from the first term and none from the second?

    Thread Starter joeybottle

    (@joeybottle)

    Yeah thats a good question! Right now after I have a lot of posts with a lot of Terms in that Taxonomy it will alway end up with 4 Posts each from a different Term.

    And if the first term has 5 posts and the second has 3, to just show 4 from the first term isn’t really the best solution too for showing “Related Post”….

    What would you suggest ??? The Solution as I got it right now, or the second one (you and I just described)…

    OR ist there a possibility to mix up random posts from different terms but with a maximum of 4 Posts???

    It really depends on what you want.

    It is possible to have random posts but it will require a major change to the code. Here is an outline of what would be needed.

    • Initialize an empty array to hold the posts.
    • Take the counter out.
    • Set showposts (better to use posts_per_page) to 4.
    • In the Loop, don’t display the posts, add them to the array.
    • Once all posts have been collected, shuffle the array.
    • Create a new for loop to display the first 4 posts from the shuffled array.
    Thread Starter joeybottle

    (@joeybottle)

    Sounds great! Best thing would be to first hold about 2 or 3 Posts for each Term which is added to the Post and then show 4 random posts out of those posts. Is that possible? Because the way you described it, I don’t really understand how many posts are in the “hold” before the new loop shows 4 random posts out of them…waiting for your answer! Thanks a lot so far!

    Just set posts_per_page to the maximum you want from each term.

    in case this is not resolved:

    <?php
    global $post;
    $terms = get_the_terms( $post->ID , 'workcap' );
    $do_not_duplicate[] = $post->ID;
    $all_post_terms = ''; 
    
    if(!empty($terms)){
    foreach ($terms as $term) {
    $all_post_terms .= $term->slug . ', ';
    } // foreach ends here - remove the 'old' closing bracket after your loop
    // the above is building a string of all post's terms of the 'workcap' taxonomy, to be used in the query;
    
    query_posts( array(
    'workcap' => $all_post_terms,
    '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; ?>

    (not widely tested)

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Limit a query to just 4 Posts, not very usual Query!!!!’ is closed to new replies.