• smizmar

    (@smizmar)


    I hope this is not something too difficult to achieve, at least it seems like a pretty common task to me.

    So I have my regular post and the posts of the wonderful All In One Calendar plugin (custom post type, of course). My goal is to show posts on my homepage of both but only from certain categories.

    I have a category for my regular posts called frontr and another one for my calendar posts called frontc. So far I am able to display all posts from both types with this marvelous piece of coding:

    function edit_my_query($query) {
      if ( ( is_home() || is_feed() || is_category() || is_tag() )
              &&  empty( $query->query_vars['suppress_filters'] ) ) {
        $post_type = get_query_var('post_type');
        if($post_type && $post_type[0] != 'post') {
          $post_type = $post_type;
        } else {
          $post_type = array('post','ai1ec_event'); // add custom post types here
        }
        $query->set('post_type',$post_type);
        if (is_category() || is_tag()) {
        // Add custom taxonomies to category and tag pages
        if (is_category()) {
            $taxonomy1 = 'category';
            $taxonomy2 = 'events_categories';
          }
          if (is_tag()){
            $taxonomy1 = 'post_tag';
            $taxonomy2 = 'events_tags';
          }
          $queried_object = $query->get_queried_object();
          $slug = $queried_object->slug;
          $query->set('tax_query', array(
            'relation' => 'OR',
            array(
              'taxonomy' => $taxonomy1,  'field' => 'slug', 'terms' => $slug
            ),
            array(
              'taxonomy' => $taxonomy2, 'field' => 'slug', 'terms' => $slug
            )
          ));
        }
      }
    }
    add_action('pre_get_posts', 'edit_my_query');

    Now I tried to narrow the results down to my categories. This one filters my regular posts and displays only the posts from the category frontr:

    <?php
    function childtheme_cat_limited_blog( $query ) {
     if ( $query->is_home() && $query->is_main_query() ) {
     $query->set( 'category_name', 'frontr' );
     }
    }
    add_action( 'pre_get_posts', 'childtheme_cat_limited_blog' );
    ?>

    The other one shows the calendar posts from the category frontc:

    <?php
    function childtheme_cat_limited_blog( $query ) {
     if ( $query->is_home() && $query->is_main_query() ) {
     $query->set( 'events_categories', 'frontc' );
     }
    }
    add_action( 'pre_get_posts', 'childtheme_cat_limited_blog' );
    ?>

    They’re working fine separately, but when I try to merge the two queries all I get is a blank page:

    function childtheme_cat_limited_blog( $query ) {
     if ( $query->is_home() && $query->is_main_query() ) {
     $query->set( 'category_name', 'frontr' );
     $query->set( 'events_categories', 'frontc' );
     }
    }
    add_action( 'pre_get_posts', 'childtheme_cat_limited_blog' );

    It doesn’t seem too hard to make it work but I can’t figure it out for the life of me, so any help would be greatly appreciated!

  • The topic ‘Showing posts on homepage from different categories of different post types’ is closed to new replies.