简体   繁体   中英

Why aren't my WordPress category posts listing on their category pages?

I've been spinning my wheels on this problem for way too long and I'm running out of time. So I turn to you, Stack Users.

Some background: This site omits certain categories depending on how you've chosen to view the site. Middle/High School users see certain categories and posts, Adult Ed users see posts and categories that MS/HS students don't. The first part of this function works. The rest do not and I cannot figure out why. The first part of the function filters out the necessary posts, including events and videos. The last two conditions restore them if you're on their respective category pages. Or, it should. That's the intention. It isn't.

function filter_posts( $query ) {
if ( ( $query->is_home() || is_category() ) && $query->is_main_query() ) :
    if ( isset( $_COOKIE['branch'] ) ) :
        $postsToExclude = array('15','10','16');
        if ( $_COOKIE['branch'] == 'middle-high-school' ) :
            array_push($postsToExclude, '69','36','70');
        elseif ( $_COOKIE['branch'] == 'adult-education' ) :
            array_push($postsToExclude, '35', '40', '68', '36','70');
        endif;

        $query->set( 'category__not_in', $postsToExclude );
    endif;
endif;

if ( $query->is_front_page() && $query->is_main_query() ) :
    // We also need to exclude the featured post from the main blog feed.
    $custom_meta[] = array(
        'key' => 'blog_feature_this',
        'value' => '"Yes"',
        'compare' => 'NOT IN'
    );
    $query->set('meta_query', $custom_meta);
endif;

if ( is_category( 'events' ) && $query->is_main_query() ) :
    $query->set( 'category__in', array(10) );
endif;

if ( is_category( 'videos' ) && $query->is_main_query() ) :
    $query->set( 'category__in', array(16) );
endif;
}
add_action( 'pre_get_posts', 'filter_posts' );

Try something like this instead - using just the one parameter and adjusting the array of values as needed.

function filter_posts( $query ) {

  // Define the array
  $postsToExclude = array();

  if ( ( $query->is_home() || is_category() ) && $query->is_main_query() ) :
      if ( isset( $_COOKIE['branch'] ) ) :
          $postsToExclude = array('15','10','16');
          if ( $_COOKIE['branch'] == 'middle-high-school' ) :
              array_push($postsToExclude, '69','36','70');
          elseif ( $_COOKIE['branch'] == 'adult-education' ) :
              array_push($postsToExclude, '35', '40', '68', '36','70');
          endif;
      endif;
  endif;

  if ( $query->is_front_page() && $query->is_main_query() ) :
      // We also need to exclude the featured post from the main blog feed.
      $custom_meta[] = array(
          'key' => 'blog_feature_this',
          'value' => '"Yes"',
          'compare' => 'NOT IN'
      );
      $query->set('meta_query', $custom_meta);
  endif;

  if ( is_category( 'events' ) && $query->is_main_query() ) :
    // If the category is in the exclude list remove it
    if(array_key_exists('10',$postsToExclude)) {
      unset($postsToExclude['10'];
    }
  endif;

  if ( is_category( 'videos' ) && $query->is_main_query() ) :
    // If the category is in the exclude list remove it
    if(array_key_exists('10',$postsToExclude)) {
      unset($postsToExclude['16'];
    }
  endif;

  // If there are some values
  if(!empty($postsToExclude)) {
    $query->set( 'category__not_in', $postsToExclude );
  }

}
add_action( 'pre_get_posts', 'filter_posts' );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM