Title: Custom query breaks template
Last modified: August 30, 2016

---

# Custom query breaks template

 *  Resolved [jenkinzz](https://wordpress.org/support/users/jenkinzz/)
 * (@jenkinzz)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/custom-query-breaks-template/)
 * Hi if you go to [http://sowingkindness.com/most-inspirational-stories-for-kids/](http://sowingkindness.com/most-inspirational-stories-for-kids/)
   you’ll see the layout is broken by a custom query I’m trying to run, sorting 
   posts by a meta value.
 * Some posts are span8 and others are span12, sometimes the sidebar just doesn’t
   show up.
 * My query is
 *     ```
       $query = new WP_Query(array(
           'post_status' =>'publish',
           'post_type' =>'post',
           'orderby' => 'meta_value_num',
           'meta_key' => '_liked',
           'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1
           ));
       ```
   
 * I put it before anything else, right after the template name.
 * Then I call `if ( $query->have_posts() ) :` etc etc

Viewing 1 replies (of 1 total)

 *  Thread Starter [jenkinzz](https://wordpress.org/support/users/jenkinzz/)
 * (@jenkinzz)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/custom-query-breaks-template/#post-6721174)
 * Resolved!
 * For future reference, I noticed that there was some conditional logic giving 
   the posts a class of span12, aka full-width, and when that happened some more
   conditional logic turned off the sidebar.
 * What I did was just replace it with static data through trial and error, comparing
   it to tempate-right-sidebar.php . Here’s what I have right now:
 * *For some reason, the comments are showing up on the first post but none of the
   others. I would like it to show on none of them, but I’ve given up for now. I’m
   calling it a feature rather than a bug.
 *     ```
       <?php
       /*
       Template Name: Top Rated Posts
       */
       	$query = new WP_Query(array(
           'post_status' =>'publish',
           'post_type' =>'post',
           'orderby' => 'meta_value_num',
           'meta_key' => '_liked',
           'paged' => (get_query_var('paged')) ? get_query_var('paged') : 1
           ));
   
       ?>
   
       <?php get_header(); ?>
   
       <div class="container">
   
       	<div class="row" id="blog" >
   
               <div class="span8">
   
               	<div class="row">
   
           <?php if ( $query->have_posts() ) :  ?>
   
               <div <?php post_class(); ?> >
   
       			     <article class="article category">
   
       				       <p>Most Inspiring Stories</p>
   
       			     </article>
   
           	  </div>
   
       		<?php while ( $query->have_posts() ) : $query->the_post(); ?>
   
                   <div class="pin-article span8 right-sidebar" >
   
       				<?php do_action('suevafree_postformat');  ?>
   
                     <div style="clear:both"></div>
   
                   </div>
   
       		<?php endwhile; else:  ?>
   
                   <div class="pin-article span8 right-sidebar" >
   
                       <article class="article category">
   
                           <h1> Not found </h1>
   
                           <p><?php _e( 'Sorry, no posts matched into ',"suevafree" ) ?> <strong>: <?php the_category(' '); ?></strong></p>
   
                       </article>
   
                   </div>
   
       	<?php endif;?>
   
                   </div>
   
               </div>
   
               <section id="sidebar" class="pin-article span4">
   
                   <div class="sidebar-box">
   
                       <?php if ( is_active_sidebar('category-sidebar-area') ) {
   
                           dynamic_sidebar('category-sidebar-area');
   
                       } else {
   
                           the_widget( 'WP_Widget_Archives','',
                           array('before_widget' => '<div class="widget-box">',
                                 'after_widget'  => '</div>',
       						  'before_title'  => '<h4 class="title">',
       						  'after_title'   => '</h4>'
                           ));
   
                           the_widget( 'WP_Widget_Calendar',
                           array("title"=> __('Calendar','suevafree')),
                           array('before_widget' => '<div class="widget-box">',
                                 'after_widget'  => '</div>',
       						  'before_title'  => '<h4 class="title">',
       						  'after_title'   => '</h4>'
                           ));
   
                           the_widget( 'WP_Widget_Categories','',
                           array('before_widget' => '<div class="widget-box">',
                                 'after_widget'  => '</div>',
       						  'before_title'  => '<h4 class="title">',
       						  'after_title'   => '</h4>'
                           ));
   
                        }
   
                        ?>
   
                   </div>
   
               </section>
   
           </div>
   
       </div>
   
       <?php
   
       	get_template_part('pagination');
       	get_footer();
   
       ?>
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘Custom query breaks template’ is closed to new replies.

 * ![](https://i0.wp.com/themes.svn.wordpress.org/suevafree/3.9.5.4/screenshot.png)
 * SuevaFree
 * [Support Threads](https://wordpress.org/support/theme/suevafree/)
 * [Active Topics](https://wordpress.org/support/theme/suevafree/active/)
 * [Unresolved Topics](https://wordpress.org/support/theme/suevafree/unresolved/)
 * [Reviews](https://wordpress.org/support/theme/suevafree/reviews/)

## Tags

 * [custom](https://wordpress.org/support/topic-tag/custom/)
 * [template](https://wordpress.org/support/topic-tag/template/)

 * 1 reply
 * 1 participant
 * Last reply from: [jenkinzz](https://wordpress.org/support/users/jenkinzz/)
 * Last activity: [10 years, 10 months ago](https://wordpress.org/support/topic/custom-query-breaks-template/#post-6721174)
 * Status: resolved