0

How can I exclude one category from this loop wher I list my custom type projects by year ?

<?php foreach(posts_by_year() as $year => $posts) : ?>
  <h3><?php echo $year; ?></h3>

  <ul>
    <?php foreach($posts as $post) : setup_postdata($post); ?>
      <li>
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
      </li>
    <?php endforeach; ?>
    <div class="et_pb_column et_pb_column_4_4"><hr class="et_pb_space et_pb_divider"style="border-color: #f7f7f9;"></div>
  </ul>
<?php endforeach; ?>

update, the function :

function posts_by_year() {
  // array to use for results
  $years = array();

  // get posts from WP
  $posts = get_posts(array(
    'numberposts' => -1,
    'orderby' => 'post_date',
    'order' => 'ASC',
    'post_type' => 'project',
    'post_status' => 'publish',
    'category' => -27
  ));

  // loop through posts, populating $years arrays
  foreach($posts as $post) {
    $years[date('Y', strtotime($post->post_date))][] = $post;
  }

  // reverse sort by year
  krsort($years);
  return $years;
}
1
  • You need to write filtering code in the function posts_by_year. Only after filtering you need to return output from the function. Sep 8, 2014 at 17:49

1 Answer 1

1

I believe you are using the following code from this pastebin link

function posts_by_year() {
  // array to use for results
  $years = array();
 
  // get posts from WP
  $posts = get_posts(array(
    'numberposts' => -1,
    'orderby' => 'post_date',
    'order' => 'ASC',
    'post_type' => 'my-custom-post-type',
    'post_status' => 'publish'
  ));
 
  // loop through posts, populating $years arrays
  foreach($posts as $post) {
    $years[date('Y', strtotime($post->post_date))][] = $post;
  }
 
  // reverse sort by year
  krsort($years);
  return $years;
}

This function uses get_posts to return the list of posts. To exclude a category, you can pass the category parameter to your arguments. Just one note

Note: The category parameter needs to be the ID of the category, and not the category name.

Note: The category parameter can be a comma separated list of categories, as the get_posts() function passes the 'category' parameter directly into WP_Query as cat.

EDIT

Please note, to exclude a category, you need to use the minus (-) sign in front of the category ID. Something like this will do. Just change -13 to the -ID where ID is the ID of the category you want to exclude

function posts_by_year() {
  // array to use for results
  $years = array();
 
  // get posts from WP
  $posts = get_posts(array(
    'numberposts' => -1,
    'orderby' => 'post_date',
    'order' => 'ASC',
    'post_type' => 'my-custom-post-type',
    'post_status' => 'publish',
    'category' => -13
  ));
 
  // loop through posts, populating $years arrays
  foreach($posts as $post) {
    $years[date('Y', strtotime($post->post_date))][] = $post;
  }
 
  // reverse sort by year
  krsort($years);
  return $years;
}

EDIT 2

Copy and paste this code as is. You had a syntax error, that is why you got the blank page

function posts_by_year() {
  // array to use for results
  $years = array();

  // get posts from WP
  $posts = get_posts(array(
    'numberposts' => -1,
    'orderby' => 'post_date',
    'order' => 'ASC',
    'post_type' => 'project', 
    'post_status' => 'publish',
    'category' => -27
  ));

  // loop through posts, populating $years arrays
  foreach($posts as $post) {
    $years[date('Y', strtotime($post->post_date))][] = $post;
  }

  // reverse sort by year
  krsort($years);
  return $years;
}

EDIT 3

It seems that you are making use of a custom taxonomy. If this is the case, make use of tax_query For available parameters, see WP_Query. You just need to add the name of your taxonomy in the taxonomy parameter

function posts_by_year() {
  // array to use for results
  $years = array();

  // get posts from WP
$posts = get_posts(array(
    'numberposts' => -1,
    'orderby' => 'post_date',
    'order' => 'ASC',
    'post_type' => 'project', 
    'post_status' => 'publish',
    'tax_query' => array(
        array(
            'taxonomy' => 'NAME OF YOUR CUSTOM TAXONOMY',
            'field'    => 'term_id',
            'terms'    => '27',
            'operator' => 'NOT IN'
        ),
    ),
  ));

  // loop through posts, populating $years arrays
  foreach($posts as $post) {
    $years[date('Y', strtotime($post->post_date))][] = $post;
  }

  // reverse sort by year
  krsort($years);
  return $years;
}
10
  • Thank you ! That is the function I am using. I will try. Don t know how to pass the category, somthing like : $args = array( 'exclude' => '27' ); ? (this doesn't work)
    – jojo79
    Sep 8, 2014 at 17:55
  • See my update, :-) Sep 8, 2014 at 18:02
  • Thank you very much but it is not working ! blank page...
    – jojo79
    Sep 8, 2014 at 18:12
  • Just updated my first post, post from category 27 are still displaying
    – jojo79
    Sep 8, 2014 at 18:17
  • Thank you, no blank page but still posts from cat 27 !
    – jojo79
    Sep 8, 2014 at 18:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.