• Resolved travis182

    (@travis182)


    Hi, i’m looking for a piece of code to put in the sidebar that lists all post that are in the current category.
    There’s a lot of examples that do this, but i need one that lists the post that are in the same combination of categories. For example if you visit the page …/category/aaa/bbb/ the sidebar lists al posts that are in catogry aaa AND bbb. All code i’ve seen so far list posts that are in category aaa OR bbb or that only take the first category.
    Like the one i’m using now;

    <?php
        $category = get_the_category(); //get first current category ID
        $this_post = $post->ID; // get ID of current post
        $posts = get_posts('orderby=post_date&category=' . $category[0]->cat_ID);
       ?>
      <?php
        foreach($posts as $post) {
       ?>
         <li><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title('')?></a></li>
       <?php } wp_reset_postdata(); ?>

    Does anyone have a clue?

Viewing 5 replies - 1 through 5 (of 5 total)
  • there is a category__and parameter for queries:

    https://codex.www.ads-software.com/Class_Reference/WP_Query#Category_Parameters

    example –
    instead:
    $posts = get_posts('orderby=post_date&category=' . $category[0]->cat_ID);

    try:

    $cats = wp_get_post_categories($post->ID);
    $args = array(
    'orderby' => 'post_date',
    'category__and' => $cats
    );
    $posts = get_posts( $args );

    https://codex.www.ads-software.com/Function_Reference/wp_get_post_categories

    https://codex.www.ads-software.com/Template_Tags/get_posts

    Thread Starter travis182

    (@travis182)

    Thanx for the tip, I’ll look into that. Your example is working for the post-pages and for the …category/aaa/bbb/ pages, but is giving a problem with the …category/aaa/ pages, because it takes the second category from the blogposts on that page and in that case I do want to show all the blogposts in category aaa. Because of the pagination, page 1 consists of only aaa/bbb posts, but the second page also shows aaa/ccc post, so in the list I would like to see aaa/bbb aswell as aaa/ccc, but now the list changes along with the pagination.
    I hope you still follow what I’m saying.

    my mistake – i thought you were talking about posts categories;

    if you are actually talking about the category archive page category with all its parents, and the psots need to match the whole ancestry:

    try and use:

    https://pastebin.com/x4yzFg9z

    (wrapped into a conditional ‘is_category()’ because it does not make sense otherwise)

    Thread Starter travis182

    (@travis182)

    I have a menu-structure with a parent category 1, child categories 1a, 1b, 1c and finally the actual blogposts in those combination of categories.
    So the code has to work on the archive pages, but also on the page of the blogpost. I now use this code, which works fine for me. Thanx for the help.

    <?php  if( is_category() ) : //because this makes only sense in a category archive page
    $cats = array();
    $cat = get_query_var('cat');
    do {
    $cats[] = (int)$cat;
    $cat = get_category($cat)->category_parent;
    }
    while($cat);
    
    $args = array(
    'category__and' => $cats,
    'orderby' => 'post_date'
    );
    $posts = get_posts( $args ); ?>
    <ul>
     <?php   foreach($posts as $post) {
       ?>
         <li><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title('')?></a></li>
       <?php } wp_reset_postdata(); ?>
    </ul>
    <?php elseif(is_single() ) : //on single blogpost-page ?>
    
    <ul>
       <?php
        $category = get_the_category(); //get first current category ID
        $this_post = $post->ID; // get ID of current post
        $cats = wp_get_post_categories($post->ID);
    $args = array(
    'orderby' => 'post_date',
    'category__and' => $cats
    );
    $posts = get_posts( $args );
       ?>
      <?php
        foreach($posts as $post) {
       ?>
         <li><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title('')?></a></li>
       <?php } wp_reset_postdata(); ?>
    </ul>
    
    <?php endif; //ends: if( is_category() ) ?>

    @travis182:

    I’ve been looking for a way to exclude a page and all its children, including the grand-children, automatically from search results. I noticed you were looking for this as well (a year ago).

    I came up with this solution:

    https://2.ly/qaw7

    I was wondering if you had already found your solution and if it’s better (more efficient) than the one I did?

    PS: Sorry if I had to reply here. The topic where your post was is closed.

    Cheers!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘list posts in ALL current categories’ is closed to new replies.