Sorting WP-QUery based on complex criteria
-
Some of my posts have meta “gc_event_date” (Ymd format / 20240527) from ACF, others have it blank.
I want to sort my WP_Query using post date where event date is blank, otherwise by event date.
I figure thats too complex for $args, so planned to use usort after the query (before the loop) with a custom function.
I’m sure no expert at this though, and as soon as un-comment pretty much ANY of the below code, my page falls over.
Any assistance would be appreciated.
global $dps_listing; $dps_listing = new WP_Query( apply_filters( 'display_posts_shortcode_args', $args, $original_atts ) ); //my code from here down usort($dps_listing->posts,'comparefunction'); ... ... ... function comparefunction($element1, $element2) { $adate1 = date('Ymd',$element1->$post_date); $edate1 = get_post_meta( $element1->$ID, 'gc_event_date', true ); // Also in 'Ymd' format eg. 20240527 echo $edate1; if( ! empty( $edate1)){ $adate1 = $edate1}; $adate2 = date('Ymd',$element2->$post_date); $edate2 = get_post_meta( $element2->$ID, 'gc_event_date', true ); if( ! empty( $edate2)){ $adate2 = $edate2}; return strcmp($adate1,$adate2); }
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘Sorting WP-QUery based on complex criteria’ is closed to new replies.