0

I'm trying to show post order by a date custom field that are displayed like this:

    Year
     Month
       Custom field post info(date, time, venue, and category)
       Title

I found this function in the forum and modified it a little bit, and works great to sort out and display by year, month and date, but now that I have the posts sorted out, I'd like to add the title of each post and the rest of the info So what I need is either a way to display that info inside one of those foreach statemets OR to have an array of ordered post IDs (I think that option would be easier for me)

<?php

        $posts = get_posts(array(
            'post_type' => 'post',
            'meta_key'  => 'fecha',
            'orderby'   => 'meta_value',
            'order'     => 'ASC',
            'meta_type' => 'DATE'
        ));

        $group_posts = array();

        if( $posts ) {

            foreach( $posts as $post ) {

                $date = CFS()->get( 'fecha' );

                $date = new DateTime($date);

                $year = $date->format('Y');
                $month = $date->format('F');

                $group_posts[$year][$month][] = array($post, $date);

            }

        }

        foreach ($group_posts as $yearKey => $years) {

            echo $yearKey;
            echo '<br>';

            foreach ($years as $monthKey => $months) {

                echo $monthKey;
                echo '<br>';

                foreach ($months as $postKey => $posts) {

                    echo $posts[1]->format('d-m-Y');
                    echo '<br>';
                    echo $posts[0]->title;
                    echo '<br>';
                }

            }

        }

    ?>

Any help would be appreciated !

  • You should already have that information - you are adding the whole $post to $group_posts, so you can get all the information from it when you are displaying it in your foreach($months...) loop. $posts[0] is the post iteself and you are already getting the title: echo $posts[0]->title;, so you can also get any other information from it as normal. – FluffyKitten Sep 29 '17 at 2:10
  • For some reason the echo $posts[0]->title; didn't return anything so I was a little bit confused. Ended up solving it in a different way – CarlaG Sep 29 '17 at 15:34
0

I figured it out afterwards. Ended up using a different approach, I'm posting it in case someone has the same issue

 <?php

    date_default_timezone_set('America/Argentina/Buenos_Aires');


    $posts_by_date_field = new WP_Query(array(
        'post_type' => 'post',
        'posts_per_page' => -1,
        'meta_key' => 'fecha',
        'orderby' => 'meta_value',
        'order' => 'ASC',
        'meta_type' => 'DATE'
    ));


    if ($posts_by_date_field->have_posts()) : while ($posts_by_date_field->have_posts()) : $posts_by_date_field->the_post();

            // LOOP

    $today = date("Y-m-d");
    $date = CFS()->get('fecha');

    if ($date >=  $today) :


            $date = new DateTime($date);


            $year = $date->format('Y');
            $month = $date->format('F');

            if ($aux_year != $year) {
                ?>
                <div class="anio"> <?php echo $year; ?> </div>
                <?php
            }

            if ($aux_month != $month) {

                ?>
                <div class="mes"> <?php echo $mes ?> </div>
                <?php
            }
            ?>

            <div class="metaData">

           <p> <?php the_title(); ?></p>

                <p> <?php echo  CFS()->get('fecha'); ?> </p>
                <p> <?php echo CFS()->get('hora'); ?> </p>
                <p> <?php echo CFS()->get('lugar'); ?> </p>
            </div>

            <?php
            $aux_year = $year;
            $aux_month = $month;

             endif;
        endwhile;
    endif;
    ?>  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.