• Resolved dgissen

    (@dgissen)


    Apologies for the long post:

    I have been using the excellent three column post function developed by Michael (@alchymyth) on several sites.
    Here it is formatting properly on a home page of a test site; it also formats properly on search, any taxonomy, etc. (when using those conditional arguments). But it doesn’t format properly when used for a single post with a related query (see here). I’ve also noticed that the formatting doesn’t work properly on a page template with post queries. I think I’ve tried every possible conditional argument, but notice how on the latter example none of the related posts below the single post are assigned the ‘column-post-left’ class. I’m doing something incorrectly, but what?

    Here are the functions and arguments I’m using below :

    functions.php

    add_filter('post_class','category_three_column_classes');
     
    function category_three_column_classes( $classes ) {
    global $wp_query;
    if( is_post_type_archive('products') || is_tax () || is_home() || is_singular('products') || is_search() || is_page(1689)  
    ) :
    $classes[] = 'three-column-post';
    if( $wp_query->current_post%3 == 0 ) $classes[] = 'column-post-left';
    endif;
    return $classes;
    }

    single-products.php

    <div id="primary" class="site-content">
    		<div id="content" role="main">
    
    			<?php while ( have_posts() ) : the_post(); ?>
    
    			<?php get_template_part( 'content-products', get_post_type() ); ?>
    			
    			
    				<?php comments_template( '', true ); ?>
    
    			<?php endwhile; // end of the loop. ?>
    			
    	</div><!-- #content -->
    	</div><!-- #primary -->	
    
    	<div id="primary" class="site-content">
    		<div id="content" role="main">
    		
    <?php $this_post = $post->ID;
    $args = array(
        'post_type' => 'products', 
        'posts_per_page' => -1, 
        'orderby' => 'title', 
        'order' => 'ASC', 
        'post__not_in' => array($this_post), 
      );
    
    $terms = get_the_terms($this_post,'region');
    if (!is_wp_error($terms)) {
      $terms = wp_list_pluck($terms,'term_id');
      $args['tax_query'] = array(
        array(
          'taxonomy' => 'region',
          'field' => 'id',
          'terms' => array_values($terms)
        )
      );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) { 
      echo '<h1 class="entry-title"> "Related Regional Products" </h1>' . '<br>' . '<br>' ; 
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
    	<?php get_template_part( 'content-archive-long' ); ?> 
    <?php
      endwhile;
    }
    wp_reset_query();  
    ?>	
    <?php } ?>
    
    	</div><!-- #content -->
    	</div><!-- #primary -->	'
    
    CSS
    

    three-column-post { width: 31.0%; float: left; margin-left: 3.0%; }
    .column-post-left { clear: left; margin-left: 0; }`

    • This topic was modified 7 years, 11 months ago by dgissen.
    • This topic was modified 7 years, 11 months ago by dgissen.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    The post_class filter callback relies on the global $wp_query to do its thing. Global $wp_query generally contains the main query. When you create a secondary query with new WP_Query(), the global $wp_query is not used, so your post_class callback is working off of old, inappropriate data.

    If the main query’s loop has completed and the main query is no longer needed, you could assign your new WP_Query to the global $wp_query so that your post_class callback can still work with current data.

    Even if the main query is not yet finished, you could possibly “borrow” the global by saving its current value elsewhere, to be restored when your secondary query is done. The main loop must have no need of this variable while you’ve borrowed it.

    Thread Starter dgissen

    (@dgissen)

    Thank you for this information. My php knowledge is fairly limited; how do I assign the query in the second loop to the global query? Or, how do I write the code for your second suggestion? Very appreciative of your time on this…

    Moderator bcworkz

    (@bcworkz)

    Alter you single.php query and loop like so:

    global $wp_query;
    $wp_query = new WP_Query($args);
    //$main_query = $wp_query;
    if( $wp_query->have_posts() ) { 
      echo '<h1 class="entry-title"> "Related Regional Products" </h1>' . '<br>' . '<br>' ; 
      while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
    	<?php get_template_part( 'content-archive-long' ); ?> 
    <?php
      endwhile;
      //$wp_query = $main_query;

    Verify the template part is not using $my_query anywhere.
    If you need to “borrow” $wp_query, uncomment the two lines

    Thread Starter dgissen

    (@dgissen)

    Thank you and I’ll try this in the AM. I really appreciate it.

    Thread Starter dgissen

    (@dgissen)

    Works perfectly! Thank you very much.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Posts in Three Columns — error in query’ is closed to new replies.