luumupussi
luumupussi

Reputation: 1

Display, and sort posts from a relationship field in Wordpress (Pods & The Events Calendar)

I have created custom post types in Wordpress using Pods, and then I am also using "The Events Calendar". I am trying to link events from The Events Calendar to a custom post type called "Projects" using relationship fields.

So I have my “Projects” (CPT created by PODS) and a relationship with an event (CPT created by The Events Calendar). When you create your project you can add events to the project. The code below, which is based on this tutorial (https://docs.pods.io/tutorials/get-values-from-a-relationship-field/) retrieves the events belonging to that specific project, and the start-time of that event, and posts them in a list.

"projects" refers to the custom post type called project. "project_event" is the relationship field. "_EventStartDate" is the field containing the start date of the event.


//get Pods object for current post
                $pod = pods( 'projects', get_the_id() );
                //get the value for the relationship field
                $related = $pod->field( 'project_event' );
                //loop through related field, creating links to their own pages
                //only if there is anything to loop through
                if ( ! empty( $related ) ) { { ;
                                echo '<ul>';
                                foreach ( $related as $rel ) { 
                                    
                                                //get id for related post and put in ID
                                                //for advanced content types use $id = $rel[ 'id' ];
                                                $id = $rel[ 'ID' ];
                                                //show the related post name as link
                                                echo '<li><a href="'.esc_url( get_permalink( $id ) ).' " >'.get_the_title( $id ).'</a> ';
                                                //get the value for some_field in related post and echo it
                                                $someField = get_post_meta( $id, '_EventStartDate', true );                                      
                                                                    if( $someField != '' ) 
                                    echo date( "j.n.Y  </li>", strtotime( $someField ) ) ;
                                    
                                    ;
}
                                } //end of foreach
                } //endif ! empty ( $related )
echo '</ul>';

This code works but it retrieves the events in a random order. I would like to sort them so that it retrieves the events according to the date of the event. My PHP skills are limited so I don’t know how to achieve this.

Also “All day” -events are not shown correctly. Right now it will post the start-time which is 00:00 in fullday events… I’m not quite sure how to solve this yet.

In the official forums of PODS support they recommended the tribe_get_start_date function (https://docs.theeventscalendar.com/reference/functions/tribe_get_start_date/)

Any tips how I could or should solve this??

Upvotes: 0

Views: 1194

Answers (1)

luumupussi
luumupussi

Reputation: 1

I actually discovered that the order is not random. The events are sorted by the ID so the newest created event is always last... But the problem remains. How to sort them by date?

I found this on GitHub (https://gist.github.com/jo-snips/5112025) which contains information on how to sort events from The Events Calendar. But is it possible to include an array in the code I provided?

Upvotes: 0

Related Questions