0

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?

1 Answer 1

1

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>
3
  • But then how to do something like 'post_type=topic&posts_per_page=5&category__in='$tax_selection['categories']' or something like that?
    – wyc
    Dec 25, 2012 at 6:53
  • @janoChen I have updated my answer. See changes in front-page.php file. Use array to pass arguments into query method. Dec 25, 2012 at 12:25
  • 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?
    – wyc
    Dec 27, 2012 at 5:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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