Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

This is my front page:

<?php

/**
 * Template Name: Front Page
 *
 * @package bbPress
 * @subpackage Theme
 */

get_header(); ?>

    <div id="primary">
      <div id="content" role="main">

        <div class="articles">
        <h2><?php echo __( 'Recent Articles', 'twentyeleven' ) ?></h2>
          <?php while ( have_posts() ) : the_post(); ?>

            <?php get_template_part( 'content', 'front-page' ); ?>

            <?php comments_template( '', true ); ?>

          <?php endwhile; // end of the loop. ?>
        </div>

        <div class="articles">
          <h2><?php echo __( 'Upcoming Events', 'twentyeleven' ) ?></h2>
          <?php echo do_shortcode('
            [events_list limit="3"]
              <article class="hentry">
                <div class="entry-left">
                  #_EVENTIMAGE{60,60}
                </div>
                <div class="entry-right">
                  <h3>#_EVENTLINK</h3>
                  <p class="e-p">#_CUSTOMEXCERPT</p>
                </div>
              </article>
            [/events_list]
          '); ?>
        </div><!-- #content -->

        <div class="articles recent-forum-topics">
          <h2><?php echo __( 'Recent Forum Topics', 'twentyeleven' ) ?></h2>
          <?php // Create and run custom loop
              $custom_posts = new WP_Query();
              $custom_posts->query('post_type=topic&posts_per_page=7');
              while ($custom_posts->have_posts()) : $custom_posts->the_post();
          ?>  <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
                <div class="entry-left">
                  <h3><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyeleven' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h3>
                </div><!-- .entry-header -->
              </article>
            <?php endwhile; ?>
          </div>

        <div class="articles">
          <h2><?php echo __( 'Taiwan News', 'twentyeleven' ) ?></h2>
          <div class="entry-left">
            <?php
              include_once(ABSPATH.WPINC.'/rss.php'); // path to include script
              $feed = fetch_rss('http://www.taiwansnews.net/index.php/rss/0dd057261bcc461b'); // specify feed url
              $items = array_slice($feed->items, 0, 4); // specify first and last item
            ?>

            <?php if (!empty($items)) : ?>
            <?php foreach ($items as $item) : ?>

              <article class="hentry hentry-news">
                  <h3><a href="<?php echo $item['link']; ?>"><?php echo $item['title']; ?></a></h3>
              </article>

            <?php endforeach; ?>
            <?php endif; ?>
          </div><!-- #content -->
        </div><!-- #content -->

        <div class="articles">
          <h2><?php echo __( 'Events Calendar', 'twentyeleven' ) ?></h2>
          <div class="entry-left">
            <?php echo do_shortcode('[events_calendar long_events=0 ]'); ?>
          </div><!-- #content -->
        </div><!-- #content -->

        <div class="articles">
          <h2><?php echo __( 'Weather Forecast', 'twentyeleven' ) ?></h2>
          <div class="weather-forecast-wrapper">
            <div class="entry-left">
              <?php echo do_shortcode('[forecast location="Taipei, Taiwan" caption="Weather for Taipei" measurement="C" todaylabel="Today" datelabel="date("m/d/Y")" highlow="%%high%%°/%%low%%°" numdays="3" iconset="Incredible" class="weather-forecast" cache="true" width="100%"]'); ?>
            </div><!-- #content -->
          </div><!-- #content -->
        </div><!-- #content -->

        <div class="articles">
        <h2><?php echo __( 'My Articles', 'twentyeleven' ) ?></h2>
        <?php // Create and run custom loop
            global $tax_selection; // use your variable
            $custom_posts = new WP_Query();
            $custom_posts->query( array( 
                'post_type' => 'post', 
                'posts_per_page' => 5, 
                'category__in' => $tax_selection['categories'],
            ) );
            while ($custom_posts->have_posts()) : $custom_posts->the_post();
        ?>  <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
              <div class="entry-left">
                <h3><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyeleven' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h3>
              </div><!-- .entry-header -->
            </article>
          <?php endwhile; ?>
      </div>
      </div>
      </div><!-- #content -->
    </div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

I have this in functions.php:

add_action( 'pre_get_posts', 'do_user_post_selection' );

function do_user_post_selection( $query ) {

    if( !is_home() || !is_user_logged_in() )
        return;

    if( isset( $query->query_vars['post_type'] ) && ( 'nav_menu_item' == $query->query_vars['post_type'] ) )
        return; 
    if( 1 < did_action('wp') )
        return;
    global $current_user;

    $tax_selection = get_user_option( 'taxonomy_selection', $current_user->ID );

    if( empty( $tax_selection ) )
        return;

    if( isset( $tax_selection['categories'] ) && ( is_array( $tax_selection['categories'] ) ) && ( !empty( $tax_selection['categories'] ) ) ) 
        $query->set( 'category__in', $tax_selection['categories'] );
    if( isset( $tax_selection['post_tag'] ) && ( is_array( $tax_selection['post_tag'] ) ) && ( !empty( $tax_selection['post_tag'] ) ) ) 
        $query->set( 'tag__in', $tax_selection['post_tag'] );

    return;
}

This is making Recent Forum Topics to disappear.

How to only apply the $tax_selection; to My Articles without breaking the other posts?

share|improve this question
    
Your taxonomy query in your front page code has a category__in parameter. Why are you also trying to set it with a hook on pre_get_posts? –  s_ha_dum Dec 27 '12 at 4:47
    
@s_ha_dum Oh, I commented $query->set before and was using using that category__in, but for some reason, it stopped working. –  janoChen Dec 27 '12 at 5:01
add comment

1 Answer 1

function pre_get_posts( $query ){
    if(isset($query->getpostlist) && $query->getpostlist) {
        add_filter( 'posts_fields', 'postsetupFields', 20);    
    } else {
        remove_filter( 'posts_fields', 'postsetupFields', 20); 
    }
return $query;
}

function postsetupFields( $fields ){
    global $wpdb;
    $fields = "{$wpdb->posts}.ID";
    return $fields;
}
add_action( 'pre_get_posts', 'postsetupFields');
share|improve this answer
    
So which custom loop will this code affect? –  janoChen Dec 27 '12 at 5:32
    
Please add a description as well as simply post code. What does this do? And why name a callback the same as the hook? That looks like bad for to me. –  s_ha_dum May 6 at 10:35
    
Also, this code produces a fatal error and please explain $query->getpostlist. –  s_ha_dum May 6 at 10:43
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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