Hello John !
I finally find a solution and I post it here if it can help someone :
<?php $custom_query_args = array('numberposts' => -1,
'posts_per_page' => 10,
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'type',
'value' => 'evenement',
'compare' => '='
)
),
'meta_key' => 'start_date',
'orderby' => 'meta_value',
'order' => 'ASC',
);
// Instantiate custom query
$custom_query = new WP_Query( $custom_query_args );
// Output custom query loop
if ( $custom_query->have_posts() ) :
while ( $custom_query->have_posts() ) :
$custom_query->the_post(); ?>
// Here is your code
//Close
<?php
endwhile;
endif;
wp_reset_postdata(); ?>
I use WP_Query and resolve my problem of the double meta_key by using the meta_query to display both my posts who are type “event” and to order event by my “start_date” field.
Thank you !
Hi John,
Thanks for your help again !
The value returned for the
echo get_field('end_date', false, false);
is the end date that I fill in the backend and with this code it gives me the date at this format : 2016-10-21 15:00:00 (for the first event), 2017-07-15 17:30:00 (for the second event)…
For the second one :
$date_one_timestamp = strtotime(get_field('end_date', false, false));
echo $date_one_timestamp;
It gives some weird numbers like 1477062000 or 1500139800
And the third one :
echo (date('Y-m-d H:i:s', $date_one_timestamp));
It gives me exactly the same than the first one, it’s the value of the end_date field like : 2016-12-23 22:00:00
The 1970-01-01 00:00:00 is the value by default if the field is not filled.
I’ll continue to search what’s wrong in my code.
Thanks a lot John !
Yes I tried but it didn’t work, nothing appears.
The
echo '<pre>'; print_r($posts); echo '</pre>';
display me the array with all data and for the
global $post;
there’s nothing that appears.
To be sure I show you where I put it in my code :
<?php global $post; ?>
<?php $posts = get_posts(array(
'post_type' => 'post',
'posts_per_page' => -1,
'meta_key' => 'start_date',
'orderby' => 'meta_value_num',
'order' => 'DESC'
));
if( $posts ): ?>
<?php foreach( $posts as $post ):
setup_postdata( $post )
?>
<?php $now = time();
$date_one_timestamp = strtotime(get_field('end_date', false, false));
if ($now < $date_one_timestamp ) { ?>
<div class="event-total-page">
<div class="category-name-event"></div>
<div class="date-time">
<div class="date-custom-page">
<?php $dateformatstring = '<p>j</p> <p>F Y</p>'; ?>
<?php $unixtimestamp = strtotime(get_field('start_date', false, false));
echo date_i18n($dateformatstring, $unixtimestamp); ?>
<a href="<?php the_permalink(); ?>"><p class="view-event btn-primary">BEKIJK EVENT</p></a>
</div>
</div>
Even if I try it in another place it doesn’t work.
Thanks a lot John for your time.
Hello John,
Thanks a lot for your reply !
Indeed, when I tried again my code this morning there were any results, yesterday I had a loop but I probably have changed something.
So I tried what you told for the first solution and it gives me the array of each posts with their ID, post_author, post_date, post_date_gmt, post_content …
I can show you a screenshot if it’s needed.
And for your second question I have created a page templates/custom page which groups all my events (and that’s why I want them to be ordered).
Thank you for your time and if you have any idea of what can cause an error in my code I would be happy !
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.