Support » Fixing WordPress » Loop through a single category, sort results by meta_value_num

  • Resolved Runtheball

    (@eisenbart)


    I’m at a loss for why this custom loop isn’t working. Its just supposed to loop through a single category, display post thumbnail and excerpt, and sort by a meta_value_num. The sort isn’t included yet, since the basic loop itself is currently not showing anything on the page.

    Why won’t this code display posts in the ‘clients’ category?
    How would I make it order those posts via meta_value_num?

    Thank you in advance!

    <?php
    /*
    Template Name: Our Clients
    */
    get_header();?>
            <!-- BEGIN OF PAGE TITLE -->
            <?php if (have_posts()) : ?>
            <div id="page-title">
            	<div id="page-title-inner">
                    <div class="title">
                    <h1><?php the_title();?></h1>
                    </div>
                    <div class="dot-separator-title"></div>
                    <div class="description">
    			<!--[if IE]>
    				<style type="text/css">
      				 	.description {margin-top:30px;}
    				</style>
    			<![endif]-->
    
                      <?php global $post;?>
                      <?php $short_desc = get_post_meta($post->ID, '_short_desc', true ); ?>
                      <p><?php echo $short_desc;?></p>
                    </div>
                    <div class="clear"></div>
                </div>
                <div class="clear"></div>
            </div>
            <!-- END OF PAGE TITLE --> 
    
            <div id="content">
            	<div id="content-left">
                    <div class="maincontent">
                    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    
    				<article id="post-<?php the_ID(); ?>" <?php if(is_category('client')): ?>class="client-post" <?php endif;?>>
    				<div class="blog-posted">
    					<div class="image-left">
    						<?php the_post_thumbnail( 'thumbnail' ); ?>
    					</div>
    
    					<!-- This is the output of the EXCERPT -->
    					<div class="entry-summary">
    						<?php the_excerpt(); ?>
    					</div>
    				</div>
    
    				</article>
    
    				<?php endwhile; else: ?>
    
    				<p>Sorry, the client posts cannot be located</p>
    
    				<?php endif; ?>
    				</div>
                </div>
                <?php endif;?>
              <?php get_sidebar();?>             
    
            </div>
            <!-- END OF CONTENT -->
            <?php get_footer();?>

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hello, which theme/childtheme are you using?

    what it seems, there is a normal default loop, so it could work the way you intend either if you set this to be a category template, or if you’re making a page template, you have to set a custom loop to pull posts from one specific category.

    By now I can suggest to take a look at
    The Loop
    The Loop in Action
    and WP_query

    tell if you still are in doubt,
    hope this can help you

    Thank you parcodeisuoni,
    This is from the Vulcan theme. I’ve copied page.php, edited it as you see above, and saved it as our-clients.php to run only this one page of the site. So yes it is intended to be a custom loop to pull posts from one specific category.

    Ok, so it’s what you can find in
    codex.wordpress.org/Class_Reference/WP_Query

    at the beginning of the page there are a couple of examples on how to set custom loops.
    you’ll see that the loops call $args to set the behaviours of the loop.
    you can find what you’re searching a little down on the page, at

    Category Parameters

    Show posts associated with certain categories.

    cat (int) – use category id.
    category_name (string) – use category slug (NOT name).
    category__and (array) – use category id.
    category__in (array) – use category id.
    category__not_in (array) – use category id.

    anyway as a quick reference I found useful this suggestion from WP:
    Comprehensive Argument Reference by Mark Luetke

    about metadata, you’re actually making it good, but inside the loop you don’t need to reference the global $post…
    this lines of code you wrote

    <?php $short_desc = get_post_meta($post->ID, '_short_desc', true ); ?>
    <p><?php echo $short_desc;?></p>

    is enough to show the metadata for the post you’re in when making your custom loop.

    glad if it helps

    Marcello

    Sorry, I made a mistake about metadata, I just see it…
    ..is BEFORE the loop, so referencing the $post global is needed.

    for the loop part, just add to `<?php
    /*
    Template Name: Our Clients
    */
    get_header();?>
    <!– BEGIN OF PAGE TITLE –>
    <?php if (have_posts()) : ?>`

    this lines:

    <?php
    /*
    Template Name: Our Clients
    */
    get_header();?>
      <!-- BEGIN OF PAGE TITLE -->
      <?php $clients_query = new WP_Query( 'category_name=YOURCATEGORY' );
            if ($clients_query->have_posts()) : ?>

    changing YOURCATEGORY with the slug you have for clients…

    and then for the loop, using <?php while ($clients_query->have_posts()) : $clients_query->the_post(); ?>

    sorry again, hope helps

    Thank you!!!
    Your information helped me a lot.

    Here’s the revised (now fully functional) code. Maybe comparing this with my initial code will help others in similar situations.

    <?php
    /*
    Template Name: Our Clients
    */
    get_header();?>
            <!-- BEGIN OF PAGE TITLE -->
            <?php if (have_posts()) : ?>
            <div id="page-title">
            	<div id="page-title-inner">
                    <div class="title">
                    <h1><?php the_title();?></h1>
                    </div>
                    <div class="dot-separator-title"></div>
                    <div class="description">
    			<!--[if IE]>
    				<style type="text/css">
      				 	.description {margin-top:30px;}
    				</style>
    			<![endif]-->
    
                      <?php global $post;?>
                      <?php $short_desc = get_post_meta($post->ID, '_short_desc', true ); ?>
                      <p><?php echo $short_desc;?></p>
                    </div>
                    <div class="clear"></div>
                </div>
                <div class="clear"></div>
            </div>
            <!-- END OF PAGE TITLE --> 
    
            <div id="content">
            	<div id="content-left">
                    <div class="maincontent">
                    <?php
    
    		// The Query
    		$query = new WP_Query( array(
    			'category_name' => 'client',
    			'meta_key' => 'order',
    			'orderby' => 'meta_value',
    			'order' => 'ASC'
    		));
    
    		// The Loop
    		if ( $query->have_posts() ) {
    			while ( $query->have_posts() ) {
    				$query->the_post();?>
    				<div class="blog-posted">
    					<div class="image-left">
    						<?php the_post_thumbnail( 'thumbnail' ); ?>
    					</div>
    
    					<div class="entry-summary">
    						<?php the_excerpt(); ?>
    					</div>
    				</div>
    			<?php }
    		} else {
    			// no posts found
    		}
    		/* Restore original Post Data */
    		wp_reset_postdata();
    		?>		
    
    		<?php else: ?>
    
    			<p>Sorry, the client posts cannot be located</p>
    
    		<?php endif; ?>
    		</div>
                	</div>
                <?php //endif;?>
              <?php get_sidebar();?>             
    
            </div>
            <!-- END OF CONTENT -->
            <?php get_footer();?>

    I’m glad I could help you.
    If you think, you may mark this topic as resolved, it can help others to see they can find some sort of solutions here…

    have a good coding time 😉

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Loop through a single category, sort results by meta_value_num’ is closed to new replies.