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.

I took this code from this question (which shows how to let users choose which posts to display by choosing categories and tags):

Filter index page plugin

function.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;
}

I have the following in the template of my front page:

front-page.php

      <?php // Create and run custom loop
          $custom_posts = new WP_Query();
          $custom_posts->query('post_type=topic&posts_per_page=5');
          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>

How to use the value in $tax_selection in my custom loop?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

I suppose you need to make it global. It might look like:

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;

    $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;
}

front-page.php

  <?php // Create and run custom loop
      global $tax_selection; // use your variable
      $custom_posts = new WP_Query();
      $custom_posts->query( array( 
          'post_type' => 'topic', 
          '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>
share|improve this answer
    
But then how to do something like 'post_type=topic&posts_per_page=5&category__in='$tax_selection['categories']' or something like that? –  janoChen Dec 25 '12 at 6:53
    
@janoChen I have updated my answer. See changes in front-page.php file. Use array to pass arguments into query method. –  Eugene Manuilov Dec 25 '12 at 12:25
    
Thanks! It worked. –  janoChen Dec 26 '12 at 6:37
    
I noticed that $query->set( is making the other custom loop that in the same page disappear). I tried commenting them but now $tax_selection['categories'] becomes NULL. Is there any workaround for this? –  janoChen Dec 27 '12 at 5:51
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.