• Resolved jezthomp

    (@jezthomp)


    I have the following code displaying varying posts from a few different categories, however i need them displayed in date order from information put in a custom field, can this be done?

    However, i am using a custom write panel; will this have a baring..

    <?php
    
    	$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
    	$postcnt = 0; query_posts('cat=3,4,5,6&order=desc');
    	$lastPost=$wp_query->post_count;
    
    	if (have_posts()) {
    		echo '<div class="example">';
    		echo '<ul class="content_cols">';
    
    		while ( have_posts() ) : the_post(); $postcnt++; $data = get_post_meta( $post->ID, 'key', true );
    
    			$category = get_the_category(); 
    
    		echo'<li class="cat'.$category[0]->cat_ID.'">';
    
    			echo '<a href="';
    				the_permalink();
    			echo '" title="View more details of ';
    				the_title();
    			echo '"><img src="wp-content/uploads/';
    			echo $data[ 'event_image' ].'" alt = "work image"/></a>'; 
    
    			echo '<h3>';
    				the_title();
    			echo '</h3>';
    			echo '<p>'.$data[ 'event_date' ].'</p>';
    			echo '<p>'.$data[ 'event_location' ].'</p>';
    			echo '<p>'.$data[ 'event_time' ].'</p>';
    			echo '<p><a href="';
    				the_permalink();
    			echo '" title="View more details of ';
    				the_title();
    			echo '">Read More</a></p>';
    			echo '<div class="clearFix"><!--x--></div>';
    			echo '</li>';
    
    			//if the post was the 6th
    			if($postcnt % 6 ==0) {
    				//close the list, add the line,clear the float and close the div
    				echo '</ul>';
    
    				if($postcnt != $lastPost) { //lastPost contains the total number of posts
    					//only display the line div if this is not the last line of posts
    					echo '<div class="line"></div>';
    				}
    
    				echo '<div class="clearFix"><!--x--></div>';
    				echo '</div>';
    				//if the post was not the last
    				if($postcnt != $lastPost) { //lastPost contains the total number of posts
    					//create a new example div and list
    					echo '<div class="example">';
    					echo '<ul class="content_cols">';
    				}
    			}
    		endwhile; ?>
    
    		</ul>
    		<div class="clearFix"><!--x--></div>
    
    		<?php
    	}
    ?>

    So idelly they are presented in order of the custom field <?php echo $data[ 'event_date' ]; ?> which i have set up.

    How can i add that info at the top of the loop so it displays in date order and how would the date have to be written in the custom field?

    Can it even be done?

    Many thanks for any help, it would be brilliant πŸ™‚

Viewing 9 replies - 1 through 9 (of 9 total)
  • According to the query_posts() article, you would need to use something like:

    query_posts('cat=3,4,5,6&order=desc&orderby=meta_value&meta_key=event_date');

    Assuming you order=desc is still okay.

    Note that your Custom Field would need dates entered in a format such as “20091022” (for Oct 22, 2009) for the sorting to work.

    Hi

    See this Codex page (shortcut URL http://bit.ly/26oSdD )
    http://codex.ww.wp.xz.cn/Displaying_Posts_Using_a_Custom_Select_Query#Query_based_on_Custom_Field_and_Sorted_by_Value

    Note that most of the examples above this section say they do not work on WP 2.5 and up. This section does, and addresses what you are looking to do.

    Suggest you format the dates in your custom field as 2009-10-22

    Thread Starter jezthomp

    (@jezthomp)

    Thanks guys, i’ll give it a go.

    So ‘desc’ will put them in date order of my custom field, newest first to oldest last?

    Will this work for custom panels also?

    I used this tutorial for custom panels

    http://wefunction.com/2009/10/revisited-creating-custom-write-panels-in-wordpress/

    This being the code i use to print the info (although slightly different above)

    <?php echo $data[ 'event_date' ]; ?>

    Or is it only a custom fields job?

    Thanks again, hopefully i can get it to work πŸ™‚

    Thread Starter jezthomp

    (@jezthomp)

    Been trying your suggestions my code is same as above with this at top..

    <?php
    
    	$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
    	$postcnt = 0; query_posts('cat=3,4,5,6&order=desc&orderby=meta_value&meta_key=date');
    	$lastPost=$wp_query->post_count;
    
    	if (have_posts()) {

    I’ve added a custom field date to each post and given it date of 2009-10-22 then to another post giving it 2009-10-23 and then changed them to something else say 2009-11-22 and 2009-12-22 for example and they dont move they still stay in the same position…

    Not ordering by date πŸ™

    I’ve never tried the combination of category and custom value that MichaelH suggested. I’ve found the query posts parameters work correctly when used by themselves, but my experience in combining them has been mixed. Some work some don’t.

    I’d be inclined to use custom SQL on this which is why I linked you to the codex page.

    I put this together for your situation

    $querystr = "
        SELECT p.*
        FROM $wpdb->posts p, $wpdb->postmeta pm,
             $wpdb->term_relationships tr, $wpdb->terms t,
             $wpdb->term_taxonomy tt
        WHERE p.ID = pm.post_id
          AND p.post_type = 'post'
          AND pm.meta_key = 'date'
          AND p.ID = tr.object_id
          AND tr.object_id = tt.term_taxonomy_id
          AND tt.term_id = t.term_id
          AND t.term_id in (3, 4, 5)
        ORDER BY pm.meta_value DESC
        ";

    Won’t guarantee its 100% correct but it ran in MySQL

    See this section for the code you need to run this query. Substitute the query code above for the query code in the section I linked to. You can work the example code on the codes page into your existing loop. The comments there about OBJECT being required, and the need to include setup_postdata() are true.

    Just as a note, here’s the SQL WordPress uses for this query_posts:

    query_posts('showposts=-1&cat=3,4,5,6&order=desc&orderby=meta_value&meta_key=date');

    SELECT wp_posts.* FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id) JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) WHERE 1=1 AND wp_term_taxonomy.taxonomy = ‘category’ AND wp_term_taxonomy.term_id IN (‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ’11’, ’20’) AND wp_posts.post_type = ‘post’ AND (wp_posts.post_status = ‘publish’ OR wp_posts.post_status = ‘private’) AND wp_postmeta.meta_key = ‘date’ GROUP BY wp_posts.ID ORDER BY wp_postmeta.meta_value desc

    [edit note category 7,11,and 20 are child cats of 6 on my system]

    Thread Starter jezthomp

    (@jezthomp)

    Thanks once again guys, really appreciate it. πŸ™‚

    I think i’ve managed to get it working using the original suggestion…
    query_posts('cat=3,4,5,6&order=desc&orderby=meta_value&meta_key=event_date');

    So let me get this straight in my head.

    2009-10-22 will appear before 2009-10-23 and say 2009-11-22, but 2009-09-22 will appear before all of them?

    That how the dates work?

    That how the dates work?

    no

    sorted descending its
    2009-11-22
    2009-10-23
    2009-10-22
    2009-09-23

    ascending its
    2009-09-23
    2009-10-22
    2009-10-23
    2009-11-22

    That is why it was suggested you structure your dates in that way, because they can be sorted as text, character by character, left to right. When querying WP is not seeing these as dates but as strings of text.

    my solution is 2 loop, the first

    $agenda = get_posts('category=4&numberposts=10');
    	<?php
    // an empty array
    $date_order = '';
    if( $agenda ) : ?> <?php  foreach( $agenda as $post ) : setup_postdata( $post ); ?> 
    
    <?php 	$custom_date = get_post_custom_values('custom_date');
    	if (isset($custom_date[0])) {
    	$evento = strtotime($custom_date[0]);
    	} else {
       $evento = strtotime($post->post_date_gmt);
    			}
    	$date_order[$evento] = $post->ID;
    
    // now the array contain key as date and post ID as value
    	 ?>
    
    	<?php endforeach;  ?>
     	<?php endif; ?>
    
    // and the second
    
    // order the date in the custom field
    ksort($date_order);
    
    foreach ($date_order as $key => $val) {
     query_posts('p='.$val);
    global $more;
    $more = 0; 
    
    // the second Loop
    while (have_posts()) : the_post(); ?>
    // your stuff here
    	<?php endwhile; ?>
     <?php	} ?>

    in this way i can order post with or without date custom field
    there are too many query?
    it’s too many expensive?

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Sort Posts By Custom Field Date?’ is closed to new replies.