WordPress.org

Forums

Recent posts from current cat. + exclude categories (9 posts)

  1. Dave_CL
    Member
    Posted 1 year ago #

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

  2. vtxyzzy
    Member
    Posted 1 year ago #

    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'));
  3. Dave_CL
    Member
    Posted 1 year ago #

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

  4. Dave_CL
    Member
    Posted 1 year ago #

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

  5. vtxyzzy
    Member
    Posted 1 year ago #

    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.

  6. Dave_CL
    Member
    Posted 1 year ago #

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

  7. vtxyzzy
    Member
    Posted 1 year ago #

    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.

  8. Dave_CL
    Member
    Posted 1 year ago #

  9. vtxyzzy
    Member
    Posted 1 year ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic