Forums

Sort posts by a custom field (date) only if custom field exists? (1 post)

  1. irina85
    Member
    Posted 2 years ago #

    Hi WordPress community,

    I am working on setting up my own category.php where I want to sort posts by a custom field which is a date (not the publish date). However, not all posts have this custom field (only posts in child categories of a certain parent category). Hence what I need is a method how to sort posts by a certain custom field (date), but only if the posts have this custom field or else I want to have it sorted the default way (publish date). Is this possible?

    I searched the support forums and tried to connect my 2 problems (sort by custom field (date)) and check if custom field exists, but the code isn't really working. Here's what I got by trying to connect these 2 solutions together: Displaying Posts Using a Custom Select Query and Check for the existence of a custom field.

    This is what I got so far:

    <?php
    /**
     * @package WordPress
     * @subpackage Default_Theme
     */
    
    get_header(); ?>
    
    	<div id="content" class="narrowcolumn">
    
    	<?php $key="When";
    
        $querystr = "
    			SELECT wposts.*
    			FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
    			WHERE wposts.ID = wpostmeta.post_id
    			AND wpostmeta.meta_key = 'When'
    			AND wposts.post_status = 'publish'
    			AND wposts.post_type = 'post'
    			ORDER BY STR_TO_DATE(wpostmeta.meta_value, '%m/%d/%Y') ASC
    			";
    
    		$pageposts = $wpdb->get_results($querystr, OBJECT);
    
      ?>
    
    	<h2><?php single_cat_title(); ?></h2>
    
    	<?php if ($pageposts): ?>
      <?php foreach ($pageposts as $post): ?>
        <?php setup_postdata($post); ?>
    
        <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><img class="post-picture" src="<?php echo get_post_meta($post->ID, "Post Picture Thumbnail", true); ?>" alt="<?php the_title(); ?>" /></a>
    
    		<?php endforeach; ?>
    
    		<div class="navigation">
    			<div class="alignleft"><?php next_posts_link('&laquo; Older Entries') ?></div>
    			<div class="alignright"><?php previous_posts_link('Newer Entries &raquo;') ?></div>
    		</div>
    
    	<?php else : ?>
    
    		<h2 class="center">Not Found</h2>
    		<p class="center">Sorry, but you are looking for something that isn't here.</p>
    		<?php get_search_form(); ?>
    
    	<?php endif; ?>
    
    	</div>
    
    <?php get_footer(); ?>

    The custom field I want to sort by is called When. What I got with the above code is the default WordPress order however, plus I get only the posts which have the When custom field even if I am in a completely different category to which the posts aren't even assigned to (like Post A and Post B get displayed in Category D even though they only belong to Category C).

    I know this is a bit confusing, but any help is greatly appreciated! Thanks!

Topic Closed

This topic has been closed to new replies.

About this Topic