I am having trouble with a custom Wordpress / ACF loop I am working on.
The idea is that it displays the latest posts within the 'events' post type, hiding any posts where the event date has passed.
The posts do hide if the date has passed. However the loop is not displaying the full amount of posts available. Currently with the loop below, it is only showing 6 out of the available 10.
I have checked the reading settings in Wordpress and that's fine.
The code I am using for my loop is:
<ul class="events-list">
<?php
$loop = new WP_Query( array(
'post_type' => 'events',
'posts_per_page' => -1,
'orderby' => 'meta_value',
'order' => 'ASC',
'meta_type' => 'DATE',
'meta_key' => 'event-date'
));
while ( $loop->have_posts() ) : $loop->the_post();
$today = date('dmY');
$expire = get_field('event-date');
if( $expire > $today )
{ ?>
<li>
<h3><?php the_field('event-date'); ?> - <?php the_title(); ?></h3>
<span class="time"><?php the_field('event-time'); ?></span>
<?php the_field('event-details'); ?>
</li>
<?php; } endwhile; wp_reset_query(); ?>
</ul>
If you're going to compare dates then you need to convert them to the appropriate types. Convert them to Unix timestamp then you can easily compare when the date has surpassed. At the moment you are comparing which string is greater than the other which works sometimes but it's much more reliable to use Unix timestamp as your date formats always need to match.
if(strtotime(get_field('event-date')) > date('U')) {
//Your code here
}
Simply print compared dates before the "if" statement, and you will see where you made a mistake.
echo $expire.'__'.$today.'<br>';
if( $expire > $today )
It can be because of invalid date format, empty $expire field etc.. Anyway, you will see what the reason is after implementing that print.
The solution to this problem was to change the loop to:
<?php
$today = date('Ymd');
$loop = new WP_Query( array(
'post_type' => 'events',
'showposts' => 2,
'meta_key' => 'event-date',
'meta_compare' => '>',
'meta_value' => date("Ymd"),
'orderby' => 'meta_value_num',
'order' => 'ASC'
));
while ( $loop->have_posts() ) : $loop->the_post();
{ ?>
Post stuff here
<?php; } endwhile; wp_reset_query(); ?>
Related
I need to display the first data acf field available from tomorrow.
For example:
I have 5 post with this data: 18/02, 20/02, 21/02, 25/02, 27/02, 28/02
Today: 18/02 => I need to display post with data 20/02 (> of today but first available)
Tomorrow: 19/02 => I need to display post with data 20/02 (> of tomowwor but first available)
20/02 => I need to display post with data 21/02 (> of data but first available)
I have this loop but I don't understand where I can insert this condition and how:
<?php
$today = date('Ymd'); // Today's date
$data = get_field(data_post_available);
$args = array(
'category_name' => 'post_available',
'orderby' => 'date',
'order' => 'ASC',
'meta_key' => 'data_post_available',
'orderby' => 'meta_value_num',
'offset' => 1
);
?>
<?php if( $the_query->have_posts() ): ?>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php endif; ?>
Can you help me?
Newbie here...
I have a custom post type of 'equipe' (team in portuguese). I am trying to sort these alphabetically by post title then display the_title so we have a alphabetical list of names.
I've done a search on here and tried a few fixes but Im struggling to get anything other that the standard order.
Any help would be much appreciated!
<?php
$args = array('orderby'=> 'title', 'order' => 'ASC', 'post_type' => 'equipe', 'posts_per_page' => -1, 'post_status' => 'publish' );
$q = new WP_Query($args);
while ( $q->have_posts() ) : $q->the_post();
?>
<h3><?php the_title(); ?></h3>
<?php
endwhile;
wp_reset_query();
?>
<?php
$args = array( 'post_type' => 'equipe', 'posts_per_page'=>5, 'orderby'=>'post_title','order'=>'ASC');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
RESOLVED:
Ok the reason it was enforcing menu_order was because of a setting (F*ing checkbox) within the plugin Post Types Order.
I needed to un-check AUTO SORT
and check Use query ASC / DESC parameter
This then allowed me to adjust the array as follow (and discussed above):
$args = array('orderby' => 'title', 'order'=>'ASC', 'post_type' => 'equipe')
However I did need to add 'order'=>'ASC' into the other pages that sorted by the original query of menu_order.
I'm using this to list all post titles...
<?php
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC'
);
query_posts($args);
if (have_posts()) :
while (have_posts()) :
the_post(); ?>
<p>
<?php the_title(); ?>
</p>
<?php endwhile;
endif;
wp_reset_query();
?>
...and that works fine.
Each of my posts also has a custom field called start_date. All these start dates are entered in the format DD-MM-YYYY (example: 10-03-2016).
What I need to be able to do (and I'm not if this can even be done) is to orderby start_date (instead of title).
To further illustrate, if there were 3 posts with start dates like this...
Post 1: 01-11-2016
Post 2: 03-01-2016
Post 3: 12-07-2016
...the posts would be displayed in the order of....
Post 2
Post 3
Post 1
...because this is in correct date order.
I hope this makes sense.
Cheers.
In wordpress WP_Query class accept args provide more helpful for post filter.
You can check in official site documentation provide custom meta fields accept in query args in order and orderby.
https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
I have two methods handle in this situation.
Your date format in start date is not properly mysql date format(YYYY-MM-DD). If you date format will same as mysql format so you need only change query args like:
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'meta_key' => 'start_date',
'post_status' => 'publish',
'orderby' => 'meta_value',
'meta_type' => 'DATE',
'order' => 'ASC'
);
If you do not want to change your date format on every each post or you have many post as difficult to change date format on each post so you can add filter to override order parameters.
Complete code
<?php
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'post_status' => 'publish',
'meta_key' => 'start_date',
'orderby' => 'title',
'order' => 'ASC'
);
add_filter( 'posts_orderby', function($orderby){
return 'STR_TO_DATE( wp_postmeta.meta_value, "%d-%m-%Y") ASC';
});
query_posts($args);
if (have_posts()) :
while (have_posts()) :
the_post(); ?>
<p>
<?php the_title(); ?>
</p>
<?php endwhile;
endif;
wp_reset_query();
?>
Hope this helpful for you sorry for poor English.
You can Query and sort posts by custom field value by just adding 'orderby' => 'name_of_your custom_field' to your WP_Query Statement.
<?php
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'name_of_your_custom_field',
'order' => 'ASC'
);
Please bear in mind that for example for a date field, the dates must be in "Ymd" format. (2022/12/09).
Hope somebody finds it helpful.
I need to have 5 random posts on post page (http://7cuteoutfits.com/2015/07/08/suits-rachel-z-office-fashion/) excluding current post. Random posts should be on chosen dates (for example posts from last 2 months until yesterday )
I added a few lines of code to single.php of my wordpress and now have 5 random posts. So I need to modify the code so that it will meet my requirements (above). I think it's 2 more lines, I'll be very thankful if you help.
<ul>
<?php
$currentID = get_the_ID();
$args = array( 'posts_per_page' => 5, 'orderby' => 'rand' );
$rand_posts = get_posts( $args);
foreach ( $rand_posts as $post ) :
setup_postdata( $post ); ?>
<li><?php the_title(); ?></li>
<?php endforeach;
wp_reset_postdata(); ?>
</ul>
You can use WP_Query for that.
global $post;
$args = array(
'post__not_in' => array($post->ID)
'orderby' => 'rand'
'date_query' => array(
array(
'after' => 'January 1st, 2015',
'before' => array(
'year' => 2015,
'month' => 07,
'day' => 9,
),
'inclusive' => true,
),
),
'posts_per_page' => 5,
);
$query = new WP_Query( $args );
This query orders randomly posts between today (inclusive) and Jan. 1st 2015
I haven't tested the snippet here, so please let me know if it does not work for you.
More info on WP_query and its usage (also for date parameters) here
Once you query with WP_Query, you have to
wp_reset_postdata();
just as you are already doing.
EDIT:
To show the post content, you can call
the_content()
to print it directly, or
get_the_content()
to get it as a return value. Then you can handle the printing later with the HTML markup you desire.
how do i filter out event posts who's date is older than the current date? my current code adds an expired class to the event if that event has expired, but becuase i need to display the next 5 upcoming posts the current code actually displays no event posts at all... here's my code....
<?php
wp_reset_query();
query_posts(array('post_type' => 'events',
'showposts' => 5,
'meta_key'=>'event_date',
'orderby' => 'meta_value',
'order' => ASC));
?>
<?php while (have_posts()) : the_post(); ?>
<?php
$eventDate = DateTime::createFromFormat('Ymd', get_field('event_date'));
$currentDate = new DateTime();
?>
<li class="<? if ($eventDate < $currentDate) { echo "expired"; } ?>">
<h4><?php the_title(); ?></h4>
<span class="date"><strong>Event Date:</strong> <? echo $eventDate->format('d/m/Y'); ?></span></li>
<?php endwhile;?>
pleas help someone :(
OK to answer your question. You can compare dates using a meta_value, but bear in mind that those dates need to be in the Mysql format YYYY-MM-DD for a comparison to be made in the database. So your custom field needs to be in that format or you need to reformat it in your code before you use it. Here's a good thread on the Wordpress Stack on that;
https://wordpress.stackexchange.com/questions/18303/fail-to-compare-dates-in-meta-query
I've not tested this but it should point you in the right direction;
$eventDate = date('Y-m-t', get_field('event_date'));
$currentDate = date('Y-m-t');
$args = array(
'post_type' => 'events',
'posts_per_page' => '-1',
'orderby' => 'meta_value',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => $eventDate,
'value' => $currentDate,
'compare' => '>',
'type' => 'DATE'
)
)
);
The other way to do it of course is just to pull all the posts into your query, set up a counter and display the first five which fulfil your criteria;
<?php
$counter = 0;
query_posts(array('post_type' => 'events',
'posts_per_page' => -1,
'meta_key'=>'event_date',
'orderby' => 'meta_value',
'order' => ASC));
while (have_posts()) : the_post();
$eventDate = DateTime::createFromFormat('Ymd', get_field('event_date'));
$currentDate = new DateTime();
if ( ($eventDate > $currentDate) && $counter<5) : ?>
<li class="<? if ($eventDate < $currentDate) { echo "expired"; } ?>">
<h4><?php the_title(); ?></h4>
<span class="date"><strong>Event Date:</strong> <? echo $eventDate->format('d/m/Y'); ?></span></li>
<?php
$counter++;
endif; ?>