Question

In wordpress I have a type of post called 'projects' and this post has a custom field called 'year'.

I would like to show all the 'projects', basically their names and description but I would like to show only once the year of execution of it. Like this

1991
project 1
project2
1995
project2.1
project 2.2
1996
project A2.1
project A2.11
1997
project AA

I have made this query

<?$wp_query->query('post_type=project_post&posts_per_page=-1&orderby=year&order=ASC');?>

But doing this I'll get this result

1991 project 1
1991 project2
1995 project2.1
1995 project 2.2
1996 project A2.1
1997 project AA

Was it helpful?

Solution 2

You should use

'post_type=project_post&posts_per_page=-1&orderby=meta_value_num&meta_key=year'

Notice orderby=meta_value_num&meta_key=year, so it'll be ordered by custom key and by default it's ASC, so no need to mention it.

Read more on Codex.

OTHER TIPS

Finally I got it

<?php
$the_query = new WP_Query( array(
    'post_type'   => 'project_post',
    'meta_key'    => 'time',
    'orderby'     => 'meta_value'
) );
$any = '';
while ($the_query->have_posts() ) :
        $the_query->the_post();
        $temp_date = get_post_meta( get_the_ID(), 'time', true );?>
    <div class="project" >
    <?if ($temp_date != $any ) {
        $any = $temp_date;?>
    <div><?echo date( "Y", strtotime( $any));?></div>
    <?}else{?><div class="field"></div><?}?>
   <div><?the_title();?> </div>
   <div><?the_field( "region" );?></div>
   </div>
   <div style="clear:both"></div>

<?endwhile;?>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top