WordPress.org

Ready to get started?Download WordPress

Forums

In the archive of one category I want to include posts from other categories. (12 posts)

  1. nikoza
    Member
    Posted 1 year ago #

    In my posts I have to categories

    (1) category A
    (2) category B

    What I am trying to accomplish is:
    In the archive page of the category A I want to include all the posts from the category B. However in the category B I want just the posts on that category.

    I am doing the following but it does not seem to work.

    function display_filters( $query ) {
    if ( $query->is_archive() && $query->query_vars['category_name'] == 'category-a') {
    $query->set( 'cat', '1,8') );
    }

    return $query;
    }
    add_action( 'pre_get_posts', 'display_filters' );

    Any help will be much appreciated.

    Cheers,
    Nick

  2. Jesús Franco
    Member
    Posted 1 year ago #

    Have you tried with subcategories?

    For example, if you have those categories:

    Mammals
    Primates

    Primates can be placed as subcategory of Mammals.

    The archive listing of Mammals would include posts under Primates category too, but Primates would show only their very own posts. You need to assign only Primates category to your posts, adding Mammals is optional.

    I suspect this is probably best suited and easier for your use case.

  3. nikoza
    Member
    Posted 1 year ago #

    Thank you Jesus Franco,

    It seems to be a work around that will do the job but I want the client to have the categories in the same level in the admin and also I would like to learn how to accomplish this with the pre_get_posts function that hooks on the default query.

    The logic behind what I am trying to do is simple however the implementation is killing me...

    Thank you very much for your kind help though!

  4. keesiemeijer
    moderator
    Posted 1 year ago #

    Try it with this [untested]:

    function display_filters( $query ) {
      // not an admin page and is the main query
      if (!is_admin() && $query->is_main_query()){
        if(is_category(1)){
          $query->set('category__in', array(1,8));
        }
      }
    }
    add_action( 'pre_get_posts', 'display_filters' );

    http://www.billerickson.net/customize-the-wordpress-query/
    http://developer.wordpress.com/2012/05/14/querying-posts-without-query_posts/

  5. nikoza
    Member
    Posted 1 year ago #

    Hello keesiemeijer,

    I just copied and pasted what you wrote above and the functions.php file but unfortunately it did not work.
    It loads only posts that are in the same category.

    I am looking at and it seems that it should work perfectly. Is there any possibility that I have set something else somewhere and that conflicts with that query?

    Thank you for your kind help!

  6. hotinthenews
    Member
    Posted 1 year ago #

    hello;

    very simple solution:

    use '&cat' followed by your category number.

    first check to see which category the user is archiving using the
    in_category() command.

    then add an if statement to the post query including the category
    numbers preceded by a minus sign to exclude it.

    <?php
    if ( in_category('B') ) {
    query_posts('&cat=category-number-for-B');
    }
    ?>

    when users use category A you do not need this statement
    since you want to list both categories.

    Hope this helps

    Lode

  7. nikoza
    Member
    Posted 1 year ago #

    Hi Lode,

    Thanks for responding. Your solution definately works and thank you for that. What I was looking for was a more elegant solution that will modify the default wordpress query before it gets executed.

    The idea behind what I am want seems very easy however the implementation does not seems to work.

    I am pretty sure that somebody should have tried the same thing however I cannot find him/her. For now when I am googling I only see people who exclude a certain category rather that including it.

    Anyway, thank you for your time, much appreciated!

  8. keesiemeijer
    moderator
    Posted 1 year ago #

    Does your theme's category template have a query_posts on the loop itself?
    http://codex.wordpress.org/Template_Hierarchy#Category_display

  9. keesiemeijer
    moderator
    Posted 1 year ago #

    Try it with this:

    function display_filters( $query ) {
      // not an admin page and is the main query
      if (!is_admin() && $query->is_main_query()){
        if(is_category(1)){
          $tax_query = array(
            'relation' => 'OR',
            array(
              'taxonomy' => 'category',
                 'field' => 'id',
                 'terms' => array(1,8)
            )
          );
          $query->set('tax_query', $tax_query);
        }
      }
    }
    add_action( 'pre_get_posts', 'display_filters' );
  10. nikoza
    Member
    Posted 1 year ago #

    Kseeriemeijer,

    That absolutely did the job!!! Thank you so much!!! I was messing so many days with such structure but was not anywhere near to it!!!

    Thank you so much for your help!

  11. nikoza
    Member
    Posted 1 year ago #

    Sorry for misspelling you name keesiemeijer!!

  12. keesiemeijer
    moderator
    Posted 1 year ago #

    No problem :-)

    I'm glad you got it resolved.

Topic Closed

This topic has been closed to new replies.

About this Topic