Support » Fixing WordPress » Recent posts from current cat. exclude categories

  • Hi there, I found that code and I am trying to exclude some categories ID.
    I use it in a single.php for most recent posts from same category like the current post is. But I have posts default in 2 groups of categories, lets say A,B,C,D and other are 1,2,3,4 … now I want to exclude all 1,2,3,4 categories and let the code display only posts from A,B,C,D category – of course just one of them for the current one.

    <h3>Recently Post From Same Category</h3>
    <ul>
    <?php
    global $post;
    $category = get_the_category($post->ID);
    $category = $category[0]->cat_ID;
    $myposts = get_posts(array('numberposts' => 5, 'offset' => 0, 'category__in' => array($category), 'post__not_in' => array($post->ID),'post_status'=>'publish'));
    foreach($myposts as $post) :
    setup_postdata($post);
    ?>
    <li>
    <a href="<?php the_permalink(); ?>">
    <?php the_title(); ?></a>
    </li>
    <?php endforeach; ?>
    <?php wp_reset_query(); ?>
    </ul>

    Any idea? Thank you!

    [Moderator Note: No bumping. If it’s that urgent, consider hiring someone.]

Viewing 8 replies - 1 through 8 (of 8 total)
  • I think you can get what you want by changing this:

    $category = $category[0]->cat_ID;
    $myposts = get_posts(array('numberposts' => 5, 'offset' => 0, 'category__in' => array($category), 'post__not_in' => array($post->ID),'post_status'=>'publish'));

    to this:

    $category = $category[0]->cat_ID;
    $cat_string = "$category,-1,-2,-3,-4";
    $myposts = get_posts(array('numberposts' => 5, 'offset' => 0, 'cat' => $cat_string, 'post__not_in' => array($post->ID),'post_status'=>'publish'));

    Well, the result is that there is no posts than … It seems to take just the first category and other just ignores.

    Nope… It just doesn´t give any posts either if the correct category is first in alphabet.

    Please post the code you are using.

    When you use the ‘cat’ parameter, any positive numbers are included and any negative numbers are excluded. So the string ‘11,6,-42, -68’ would include category ids 11 and 6, and exclude category ids 42 and 68.

    yep, i know that.. But in that code it doesn´t work properly.

    Unless you post the code you are using, I cannot suggest anything else. Please put the entire template in a pastebin and post a link to it here.

    Strange, this section of code worked for me (with my own category id numbers):

    global $post;
    $category = get_the_category($post->ID);
    $category = $category[0]->cat_ID;
    $cat_string = "$category,-15,-29,-65,-69";
    $myposts = get_posts(array('numberposts' => 5, 'offset' => 0, 'cat' => $cat_string, 'post__not_in' => array($post->ID),'post_status'=>'publish'));
    foreach($myposts as $post) :
    setup_postdata($post);
    ?>

    Maybe a plugin is interfering.

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Recent posts from current cat. exclude categories’ is closed to new replies.