Support

Account

Home Forums General Issues Sort custom posts by date from two diffrent custom field

Helping

Sort custom posts by date from two diffrent custom field

  • Hey I have problem with sort my gallery custom posts.

    I created my CPT Gallery and my CPT Event.

    Gallery have ACF field ‘date’ and field named ‘related_event’. Event have ACF field ‘start_event_date’.

    Some galleries have relationship with Events but some have not.

    If gallery have ralationship with Event date of gallery is date of Event (custom field – ‘start_event_date’) but if gallery have not relation value of date is value of own custom field ‘date’.

    I dont’t have idea how can I connect value of dates and sort by them.

    Please give me any advice or corrcet solution my issue.

    
    <?php 
                        	$argsGallery = array(
                                'post_type' => 'galeria',
                                'posts_per_page' => 6,
                                // 'meta_key' => 'date',
                                // 'orderby' => 'meta_value',
                                // 'order' => 'DESC'
                            );
                            $theGalleryPosts = new WP_Query($argsGallery);
    
                            while($theGalleryPosts->have_posts()){
                                $theGalleryPosts->the_post();
                                $title = get_the_title();
                                $mainPhoto = get_field('main_photo');
                                $date= get_field('date');
                                $relatedEvent = get_field('related_event');
                              
                                if($relatedEvent){
                                    foreach($relatedEvent as $event){
                                        $EventsID = array();
                                        $argsEvents = array(
                                            'post_type' => 'wydarzenie',
                                            'posts_per_page' => -1,
                                        );
                                        $theEventsPosts = new WP_Query($argsEvents);
                                        if ( $theEventsPosts->have_posts() ) {
                                            while($theEventsPosts->have_posts()){
                                                $theEventsPosts->the_post();
                                                if($event->ID == get_the_ID()){
                                                    $startEventDate = get_field('start_event_date');
                                      
                                                }
                                            }
                                        }
                                    }
                                }
                                ?>
    
                                <div class="gallery__card">
                                    <img class="gallery__card-img" src="<?php echo $mainPhoto['url'] ?>">
                                    <div class="gallery__card-info">
                                        <p class="gallery__card-title"><?php echo $title;?></p>
                                        <?php
                                        if ($relatedEvent){ ?>
                                            <p class="gallery__card-date"><?php echo $startEventDate ?></p>
                                        <?php } else{ ?>
                                        <p class="gallery__card-date"><?php echo $date; ?></p>
                                        <?php } ?>
                                    </div>
                                </div>
                                
                            <?php
                            }wp_reset_postdata();
                            ?>
    
    
  • You cannot posts by the date field in a different post, you can only order posts by fields that belong to those posts.

    You will need to create an acf/save_post filter for your gallery post type. In this filter you will need to see if there is a related event. If there is then you get the date from the related event and update the date field for the gallery. Then you order by this one date field.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.