Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save celticwebdesign/60b8f3438982b5cecf70eb372b57cf44 to your computer and use it in GitHub Desktop.
Save celticwebdesign/60b8f3438982b5cecf70eb372b57cf44 to your computer and use it in GitHub Desktop.
WordPress push loop results in to array of objects - based on meta field date
<?php
$args = array(
'post_type' => 'special-offers-cpt',
'post_status' => 'publish',
'posts_per_page' => -1
);
// The query
$child_query = new WP_Query($args);
$stack = array();
// The loop
if( $child_query->have_posts() ) : while ( $child_query->have_posts() ) : $child_query->the_post();
$sop_offer_ends = new DateTime(get_field('sop_offer_ends')." 23:59:59");
$today_date = new DateTime();
if ($today_date < $sop_offer_ends) {
//echo "current time is less than ".date_format($sop_offer_ends, 'd-m-Y');
array_push( $stack, (object)array(
"title" => get_the_title(),
"offer_code" => get_field('sop_offer_code'),
"offer_ends" => get_field('sop_offer_ends'),
"message" => get_field('sop_description'),
"image" => get_field('sop_offer_image'),
"link" => get_field('sop_link')
)
);
}
endwhile; endif;
// echo "<pre>";
// print_r($stack);
// echo "</pre>";
wp_reset_postdata();
?>
<?php
if (empty($stack)) {
} else {
foreach ($stack as $offer) {
echo $offer->title;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment