Jump to content

wordpress sort posts by custom field date


jarvis

Recommended Posts

Hi All,

 

I hope someone can help. I've got the following code in my index.php file to sort the posts by date d/m/y then title.

<?php $posts = query_posts( $query_string . 'meta_key=Day&meta_key=Month&meta_key=Year&order=asc' ); ?>

<?php if( $posts ) : ?>


<?php foreach( $posts as $post ) : setup_postdata( $post ); ?>

<!-- listings_wrapper -->
<div id="listings_wrapper">

  <!-- listings_content -->
  <div id="listings_content">
	<h1><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h1>
	<p><?php $key="Venue"; echo get_post_meta($post->ID, $key, true); ?></p>
	<p><span><?php $key="Date"; echo get_post_meta($post->ID, $key, true); ?></span></p>
	<p><a href="<?php the_permalink() ?>">more info</a></p>
  </div>
  <!-- /listings_wrapper_content -->
  
  <!-- listings_thumbnail -->
  <div id="listings_thumbnail">
  <?php the_post_thumbnail( 'listings-post-thumbnail' ); ?>  
  </div> 
  <!-- /listings_wrapper_thumbnail -->

</div>
<!-- /listings_wrapper -->

<?php endforeach; ?>

<?php else: ?>

<p><?php _e('Sorry, no events matched your criteria.'); ?></p>

<?php endif; ?>

This works well. I'd also like this to work on my archive.php file to replace:

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<!-- listings_wrapper -->
<div id="listings_wrapper">

  <!-- listings_content -->
  <div id="listings_content">
	<h1><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h1>
	<p><?php $key="Venue"; echo get_post_meta($post->ID, $key, true); ?></p>
	<p><span><?php $key="Date"; echo get_post_meta($post->ID, $key, true); ?></span></p>
	<p><a href="<?php the_permalink() ?>">more info</a></p>
  </div>
  <!-- /listings_wrapper_content -->
  
  <!-- listings_thumbnail -->
  <div id="listings_thumbnail">
  <?php the_post_thumbnail( 'listings-post-thumbnail' ); ?>  
  </div> 
  <!-- /listings_wrapper_thumbnail -->

</div>
<!-- /listings_wrapper -->

<?php endwhile; else: ?>

<p><?php _e('Sorry, no events matched your criteria.'); ?></p>

<?php endif; ?>

However, when i do that and search by category, it just returns nothing. As soon as I put the code back, it works but not displaying any order, it just shows the posts.

The category list is generated from:

<?php wp_list_categories( 'hide_empty=0&order=ASC&title_li&exclude=3,8' ); ?>

What am I doing wrong?

 

TIA

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.