$查询与同一页面中的其他查询冲突

时间:2012-12-27 作者:janoChen

这是我的头版:

<?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(); ?>
我在函数中有这个。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;
}
这就是Recent Forum Topics 消失。

如何仅应用$tax_selection; 在不破坏其他帖子的情况下阅读我的文章?

1 个回复
SO网友:Selva Balaji
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\');
结束

相关推荐

wp-query problem with author

我运行查询:SELECT post_author FROM `wp_posts` 看到很多帖子都有作者值1。然后执行搜索。php如下所示:$args = array( \'author\' => 1, ); $the_query = new WP_Query( $args ); 没有结果!怎么了?