Hi,
Is it possible to order posts by the value of a custom field.
I have an agenda in which i used the custom field 'datum concert' which has a value (ex) 09.02.2010.
Now I would like to order them ascending by this custom field.
This is my current code:
<?php while (have_posts()) : the_post(); ?>
<div <?php post_class() ?>>
<h2>
<div class="day"><?php $key="dag_concert"; echo get_post_meta($post->ID, $key, true); ?> </div>
<div class="date"><?php $key="datum_concert"; echo get_post_meta($post->ID, $key, true); ?></div>
<div class="clear"></div>
<div class="location"><?php the_title(); ?></div>
</h2>
<div class="information">
<span class="label">Aanvang:</span> <strong><?php $key="aanvang_concert"; echo get_post_meta($post->ID, $key, true); ?></strong><br />
<span class="label">T:</span> <?php $key="telefoon_concert"; echo get_post_meta($post->ID, $key, true); ?><br />
<span class="label">E:</span> <a href="mailto:<?php $key="email"; echo get_post_meta($post->ID, $key, true); ?>"><?php $key="email"; echo get_post_meta($post->ID, $key, true); ?></a><br />
<span class="label">W:</span> <a href="http://<?php $key="website"; echo get_post_meta($post->ID, $key, true); ?>" target="new"><?php $key="website"; echo get_post_meta($post->ID, $key, true); ?></a>
</div>
<div class="clear"></div>
</div>
<?php endwhile; ?>
Thanks for the help.
Utkarsh Kukreti answers:
Is the format of the custom field exactly the same in all your posts? Also, is it dd.mm.yyyy or mm.dd.yyyy?
Filip Van Reeth comments:
its day month year.
Yes, they are all the same.
Utkarsh Kukreti comments:
Try
<?php
global $post;
global $wpdb;
$posts = $wpdb->query("SELECT p.*
FROM $wpdb->posts p, $wpdb->postmeta m
WHERE p.ID = m.post_id
AND m.meta_key = 'datum_concert'
AND p.post_type = 'post'
AND p.post_status = 'publish'
ORDER BY
STR_TO_DATE(m.meta_value, 'd.m.Y') ASC");
foreach( $posts as $post ) : setup_postdata($post); ?>
<div <?php post_class() ?>>
<h2>
<div class="day"><?php $key="dag_concert"; echo get_post_meta($post->ID, $key, true); ?> </div>
<div class="date"><?php $key="datum_concert"; echo get_post_meta($post->ID, $key, true); ?></div>
<div class="clear"></div>
<div class="location"><?php the_title(); ?></div>
</h2>
<div class="information">
<span class="label">Aanvang:</span> <?php $key="aanvang_concert"; echo get_post_meta($post->ID, $key, true); ?><br />
<span class="label">T:</span> <?php $key="telefoon_concert"; echo get_post_meta($post->ID, $key, true); ?><br />
<span class="label">E:</span> <a href="mailto:<?php $key="email"; echo get_post_meta($post->ID, $key, true); ?>"><?php $key="email"; echo get_post_meta($post->ID, $key, true); ?></a><br />
<span class="label">W:</span> <a href="http://<?php $key="website"; echo get_post_meta($post->ID, $key, true); ?>" target="new"><?php $key="website"; echo get_post_meta($post->ID, $key, true); ?></a>
</div>
<div class="clear"></div>
</div>
<?php endforeach; ?>
Filip Van Reeth comments:
hmm, white page ;-)
Utkarsh Kukreti comments:
Could you please add the code mentioned here http://codex.wordpress.org/User:Sivel/FAQ#WordPress_White_Screen_of_Death so that I could see the exact error caused?
Filip Van Reeth comments:
Hmm, now it gives this: http://www.soulsister.be/category/agenda
Utkarsh Kukreti comments:
That's strange. Are you sure it's my code on the site right now? Could I have FTP access to the site? Message me from [[LINK href="http://wpquestions.com/user/profile/id/17"]]http://wpquestions.com/user/profile/id/17[[/LINK]]
Cosmin Popovici answers:
Put the following before the loop starts:
query_posts('meta_key=fieldnamehere&meta_value=thevalue&orderby=meta_value');
fieldnamehere = custom field name,
thevalue=thevalue of your customfield (key)
So your full code will be:
<?php query_posts('meta_key=fieldnamehere&meta_value=thevalue&orderby=meta_value'); ?>
<?php while (have_posts()) : the_post(); ?>
<div <?php post_class() ?>>
<h2>
<div class="day"><?php $key="dag_concert"; echo get_post_meta($post->ID, $key, true); ?> </div>
<div class="date"><?php $key="datum_concert"; echo get_post_meta($post->ID, $key, true); ?></div>
<div class="clear"></div>
<div class="location"><?php the_title(); ?></div>
</h2>
<div class="information">
<span class="label">Aanvang:</span> <?php $key="aanvang_concert"; echo get_post_meta($post->ID, $key, true); ?><br />
<span class="label">T:</span> <?php $key="telefoon_concert"; echo get_post_meta($post->ID, $key, true); ?><br />
<span class="label">E:</span> <a href="mailto:<?php $key="email"; echo get_post_meta($post->ID, $key, true); ?>"><?php $key="email"; echo get_post_meta($post->ID, $key, true); ?></a><br />
<span class="label">W:</span> <a href="http://<?php $key="website"; echo get_post_meta($post->ID, $key, true); ?>" target="new"><?php $key="website"; echo get_post_meta($post->ID, $key, true); ?></a>
</div>
<div class="clear"></div>
</div>
<?php endwhile; ?>
Kailey Lampert answers:
You can adjust the query to allow for ordering by a custom field
query_posts('orderby=meta_value_num&meta_key=datum concert&order=asc');
'&order=asc' added to make it have ascending order
Filip Van Reeth comments:
Hi,
We are getting close:
See the url: http://www.soulsister.be/category/agenda
Can you get it right ;-)
Filip Van Reeth comments:
He only filters on the day but not the months?
Filip Van Reeth comments:
Nopes, only takes the first 2 digits to order and forgets the months and years
Kailey Lampert comments:
Am I correct that this is "month.day.year"?
WordPress doesn't have any date-specific sorting for custom fields, so it will order them alphabetically or numerically, but not chronologically:
09.02.2010
10.02.2010
12.01.2010
if the date could be stored in string format, it could easily be sorted correctly.
Convert the date when to string saving:
<?php $str = strtotime('09.02.2010'); ?>
Display the date in human friendly format:
<?php
$str = get_post_meta($post->ID, 'datum concert', true);
$date = date( 'm.d.Y' , $str ); ?>
Filip Van Reeth comments:
Hi,
It's day month year.
Can you put it into the code i pasted?
Pau answers:
try to add this
query_posts('meta_key=datum concert&orderby=meta_value&order=desc');
Filip Van Reeth comments:
Hi,
He only filters on the day but not the months?
Pau comments:
probably because your meta value is not a valid date format but try this to see if it work
query_posts(array(
'meta_key' => 'datum concert',
'meta_value' => date('m.d.Y'),
'meta_compare' => '>',
'orderby' => 'meta_value',
'order' => 'DESC'
));
Filip Van Reeth comments:
nopes, but i have a feeling you are gonna find it.
I should get this site live by 20:00 tonight.
Big concert ;-)
Sébastien | French WordpressDesigner answers:
$datum=get_post_meta($post->ID, 'datum_concert', true);
query_posts('meta_key=$datum&orderby=meta_value_num');
Sébastien | French WordpressDesigner comments:
You use a numeric value, so you must use orderby=meta_value_num and no orderby=meta_value
$datum=get_post_meta($post->ID, 'datum_concert', true);
query_posts('meta_key='.$datum.'&orderby=meta_value_num');
Sébastien | French WordpressDesigner comments:
sorry, the code must be :
query_posts('meta_key=datum_concert&orderby=meta_value_num');
Filip Van Reeth comments:
Can you change the following code:
<?php while (have_posts()) : the_post(); ?>
<div <?php post_class() ?>>
<h2>
<div class="day"><?php $key="dag_concert"; echo get_post_meta($post->ID, $key, true); ?> </div>
<div class="date"><?php $key="datum_concert"; echo get_post_meta($post->ID, $key, true); ?></div>
<div class="clear"></div>
<div class="location"><?php the_title(); ?></div>
</h2>
<?php
$aanvang_concert = get_post_meta($post->ID, 'aanvang_concert', true);
$telefoon_concert = get_post_meta($post->ID, 'telefoon_concert', true);
$email = get_post_meta($post->ID, 'email', true);
$website = get_post_meta($post->ID, 'website', true);
?>
<div class="information">
<?php
if($aanvang_concert) { ?>
<span class="label">Aanvang:</span> <?php echo $aanvang_concert; ?><br />
<? } ?>
<?php
if($telefoon_concert) { ?>
<span class="label">T:</span> <?php echo $telefoon_concert; ?><br />
<? } ?>
<?php
if($email) { ?>
<span class="label">E:</span> <a href="mailto:<?php echo $email; ?>"><?php $key="email"; ?></a><br />
<? } ?>
<?php
if($website) { ?>
<span class="label">W:</span> <a href="http://<?php echo $website; ?>" target="new"><?php echo $website; ?></a>
<? } ?>
</div>
<div class="clear"></div>
</div>
<?php endwhile; ?>
Thanks ;-)
Sébastien | French WordpressDesigner comments:
and thanks to have choice my response in the other question "hide if custom field is empty " :-)))
Sébastien | French WordpressDesigner comments:
yes i do that now
Sébastien | French WordpressDesigner comments:
<?php query_posts('meta_key=datum_concert&orderby=meta_value_num');
if ( have_posts() ) : while ( have_posts() ) : the_post();
?>
<div <?php post_class() ?>>
<?php
$dag_concert = get_post_meta($post->ID, 'dag_concert', true);
$datum_concert = get_post_meta($post->ID, 'datum_concert', true);
?>
<h2>
<?php
if($dag_concert) { ?>
<div class="day"><?php echo $dag_concert; ?> </div>
<? } ?>
<?php
if($datum_concert) { ?>
<div class="date"><?php echo $datum_concert; ?></div>
<? } ?>
<div class="clear"></div>
<div class="location"><?php the_title(); ?></div>
</h2>
<?php
$aanvang_concert = get_post_meta($post->ID, 'aanvang_concert', true);
$telefoon_concert = get_post_meta($post->ID, 'telefoon_concert', true);
$email = get_post_meta($post->ID, 'email', true);
$website = get_post_meta($post->ID, 'website', true);
?>
<div class="information">
<?php
if($aanvang_concert) { ?>
<span class="label">Aanvang:</span> <?php echo $aanvang_concert; ?><br />
<? } ?>
<?php
if($telefoon_concert) { ?>
<span class="label">T:</span> <?php echo $telefoon_concert; ?><br />
<? } ?>
<?php
if($email) { ?>
<span class="label">E:</span> <a href="mailto:<?php echo $email; ?>"><?php $key="email"; ?></a><br />
<? } ?>
<?php
if($website) { ?>
<span class="label">W:</span> <a href="http://<?php echo $website; ?>" target="new"><?php echo $website; ?></a>
<? } ?>
</div>
<div class="clear"></div>
</div>
<?php endwhile; ?>
Sébastien | French WordpressDesigner comments:
in the code above i have insert the query post and i have add the same think that i have write in the other question, but for the key $dag_concert and $datum_concert
is it what you want ? :-)
(i am on skype but i speak only french :-/
Filip Van Reeth comments:
oepsi, white page with the code.
i'm on skype with filip4nono
Sébastien | French WordpressDesigner comments:
could you paste here all the code of your file please ?
Filip Van Reeth comments:
<?php get_header(); ?>
<div id="content">
<div id="agenda">
<h1>Agenda</h1>
<?php while (have_posts()) : the_post(); ?>
<div <?php post_class() ?>>
<h2>
<div class="day"><?php $key="dag_concert"; echo get_post_meta($post->ID, $key, true); ?> </div>
<div class="date"><?php $key="datum_concert"; echo get_post_meta($post->ID, $key, true); ?></div>
<div class="clear"></div>
<div class="location"><?php the_title(); ?></div>
</h2>
<?php
$aanvang_concert = get_post_meta($post->ID, 'aanvang_concert', true);
$telefoon_concert = get_post_meta($post->ID, 'telefoon_concert', true);
$email = get_post_meta($post->ID, 'email', true);
$website = get_post_meta($post->ID, 'website', true);
?>
<div class="information">
<?php
if($aanvang_concert) { ?>
<span class="label">Aanvang:</span> <?php echo $aanvang_concert; ?><br />
<? } ?>
<?php
if($telefoon_concert) { ?>
<span class="label">T:</span> <?php echo $telefoon_concert; ?><br />
<? } ?>
<?php
if($email) { ?>
<span class="label">E:</span> <a href="mailto:<?php echo $email; ?>"><?php $key="email"; ?></a><br />
<? } ?>
<?php
if($website) { ?>
<span class="label">W:</span> <a href="http://<?php echo $website; ?>" target="new"><?php echo $website; ?></a>
<? } ?>
</div>
<div class="clear"></div>
</div>
<?php endwhile; ?>
</div>
</div>
<?php get_footer(); ?>
Sébastien | French WordpressDesigner comments:
if you delete my code, have you a blank page ?
Filip Van Reeth comments:
I removed your code and it works fine again.
Sébastien | French WordpressDesigner comments:
there is no problem on my site with this code... you still have this problem ?
If you have still this problem i can see in your wordpress. I need your ftp id. You can mail me on maildeseb[at]gmail.com
Filip Van Reeth comments:
The problem is fixed by Utkarsh.