• Resolved lespalmeresvendrell

    (@lespalmeresvendrell)


    Hi!
    I have a type of post called ‘projects’ and this post have a field custom field called ‘year’.

    I would like to show this post group by year kind of 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 like this i get the year on every project..kind of this

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

    Thanks!

    .and that’s not what I’m spected.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter lespalmeresvendrell

    (@lespalmeresvendrell)

    I think this must be a kind of select distinct…but no idea

    Thread Starter lespalmeresvendrell

    (@lespalmeresvendrell)

    Thread Starter lespalmeresvendrell

    (@lespalmeresvendrell)

    here is the solution

    <?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;?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to group by custom field’ is closed to new replies.