Pay money and get answers for your WordPress questions (more info)

Ordering by custom date (meta) field not working?

  • SOLVED

I have a custom post type "prosjekter" where I want to order the posts by a custom date field. The date is saved as a unix timestamp, so it should be easy, but by some reason it just wont work. I also exclude those with a startdate earlier than today.

Can anyone see why?

Here is my query (and loop):


<?php

$today = date('U');


$args = array(
'post_type' => 'prosjekter',
'posts_per_page' => -1,
'meta_query' => array(
'key' => 'ecpt_konsertdato2',
'value' => $today,
'compare' => '>'
),
'orderby' => 'meta_value_num',
'meta_key' => 'ecpt_konsertdato2',
'type' => 'NUMERIC',
'order' => 'ASC'
);?>



Samlesider der du finner all info for hvert enkelt prosjekt
<hr/>
<?php
$today = date('U');
$args = array(
'post_type' => 'prosjekter',
'order' => 'ASC',
'meta_query' => array(
array(
'posts_per_page' => -1,
'key' => 'ecpt_konsertdato2',
'value' => $today,
'orderby' => 'meta_value',
'compare' => '>',
'order' => 'ASC'
)
)
);

query_posts($args);
if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div class="internliste">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?> (<?php $custom = get_post_custom($post->ID);
$date = $custom["ecpt_konsertdato2"][0];
echo date('d/m/Y', $date); ?>)</a>
</div>
<?php endwhile; wp_reset_query(); ?>

Answers (1)

2013-01-30

Dbranes answers:

Hej Torstein, what about this one:

<?php wp_reset_query(); ?>
<?php
$today = date('U');
$args = array(
'post_type' => 'prosjekter',
'posts_per_page' => -1,
'meta_key' => 'ecpt_konsertdato2',
'orderby' => 'meta_value_num',
'order' => 'ASC'
'meta_query' => array(
array(
'key' => 'ecpt_konsertdato2',
'value' => $today,
'type' => 'NUMERIC',
'compare' => '>',
)
)
);
$myquery = new WP_Query( $args );
while ( $myquery->have_posts() ) : $myquery->the_post(); ?>
<div class="internliste">
<a href="<?php the_permalink(); ?>"><?php the_title(); ?> (
<?php $custom = get_post_custom(get_the_ID());
$date = $custom["ecpt_konsertdato2"][0];
echo date('d/m/Y', $date); ?>
)</a>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>


Torstein Opperud comments:

Hehe, actually I saw it myself just after posting it here, I guess thats what you get for working triple hours for the last week. Double query. Jeez...


Torstein Opperud comments:

that might be the dumbest mistake I have ever done :)