Support » Plugins » Hacks » Custom Post Type: Order Loop if Custom Field Exsists

  • I’m trying to reorder my posts being queried on the page. If a particular custom field exist, I want those posts moved to the top of the loop order….then posts that don’t have the custom field come after. I’m thinking this will have to be done maybe by an array_merge but I’m not sure if there is another way. I’ve got this…..

    <?php while(have_posts()) : the_post(); ?>
    <div class="articleContent">
      <h3><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
        <?php the_title(); ?>
        </a></h3>
      <small>
      <?php the_excerpt_rss(); ?>
      </small>
    <?php endwhile; ?>

    I need to modify the above to check for meta_key called “order” and orderby the meta_value ASC. Every time I do it I end up just getting the one’s that have the custom field and it drops the posts that don’t.

Viewing 12 replies - 1 through 12 (of 12 total)
  • Here is a thread on a similar topic, with a solution that you can adapt to your theme.

    thanks I’ll give it a try.

    Need to run over the loop a few times (it’s not any extra queries, it just means iterating over the data a few times), but this approach should work.

    <?php
    $with_meta = array();
    
    // Loop over posts just to find out which posts have the meta key
    while(have_posts()) :
    	the_post();
    
    	$has_my_meta = get_post_meta( $post->ID, 'YOURFIELDNAMEHERE', true );
    	if( $has_my_meta )
    		// Foreach post that has the key, store the ID into the array
    		$with_meta[] = $post->ID;
    
    endwhile;
    
    // Rewind the query
    rewind_posts();
    
    // If no IDs, just do the loop as normal
    if( empty( $with_meta ) ) : 
    
    	while(have_posts()) : the_post();
    	?>
    
    		<div class="articleContent">
    			<h3><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
     			 <small><?php the_excerpt_rss(); ?></small>
    		</div>
    	<?php
    	endwhile;
    
    // Else if we have some IDs, loop over the posts skipping those not in the array
    else :
    
    	while(have_posts()) : the_post();
    		if( !in_array( $post->ID, $with_meta ) )
    			continue;
    	?>
    
    		<div class="articleContent">
    			<h3><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
     			 <small><?php the_excerpt_rss(); ?></small>
    		</div>
    
    	<?php
    	endwhile; 
    
    	// Now rewind the query again so we can iterate over the posts that don't have the key
    	rewind_posts();
    
    	while(have_posts()) : the_post();
    		if( in_array( $post->ID, $with_meta ) )
    			continue;
    	?>
    
    		<div class="articleContent">
    			<h3><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
     			 <small><?php the_excerpt_rss(); ?></small>
    		</div>
    
    	<?php
    	endwhile;
    
    endif;
    ?>

    Hope that helps.. 🙂

    @mark, I think that you will not be able to paginate using that approach.

    What makes you think that?

    If you don’t select all posts, then some of the ones with the custom field will not be in the first batch. If you do select all posts, then pagination will be difficult, I think.

    I’m not doing any post selection, the query already exists, all i’m doing here is sorting the existing query depending on whether there’s a particular custom field present, i think that’s inline with what the original question was.

    Maybe i’m reading the question differently then you are?

    🙂

    No, we are on the same page. I just assumed that pagination might be needed and wanted the OP to be aware. Your post addresses the original question very well.

    The query will be paged already, unless query parameters are being set prior to the code we’ve been shown, ie. the user has explicitly set the query arguments and negating to include paging parameters (or if there aren’t enough posts to page).

    If we assume for a moment there is no query_posts() call prior to the code shown, then paging should be fine as is..

    I guess I misstated what I was trying to get across.

    If there is paging, some of the posts having the custom field may not be in the group for the first page. In that case, they will show at the top of whichever page they happen to fall in. So, the paging works, but perhaps not all of the posts having the custom field will show before all posts without the field.

    I understand what you’re saying .. i think i just read the question slightly differently, though there’s nothing wrong with a little forward thinking, the user may very well want all the posts sorted by the meta key/value, i simply read the question as meaning the current page, and not the totality of all the posts for the query…

    🙂

    Could very well be just what you are thinking. Hope I didn’t confuse the issue.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Custom Post Type: Order Loop if Custom Field Exsists’ is closed to new replies.