List posts with multiple dates from Advanced Custom Fields - php

I have posts with ACF repeater date fields showing the beginning of an event.
Advanced custom fields has a feature which allows posts to have repeater fields added it it. So I added a repeater date field, so one post can have multiple dates associated with it.
Now I want to list events by the date. I figured out, how to list them by the first date, but how would I go on about listing all of the posts in one list?
Here is the code I have until now (I'm a noob at php):
<?php $posts = get_posts(array(
'numberposts' => -1,
'category' => array('8,11,12'),
'orderby_field' => 'trip_dates_0_start', // order of posts by this field
'orderby_type' => 'date',
'order' => 'ASC' ));
if($posts)
{ foreach($posts as $post) : ?>
<?php while(the_repeater_field('trip_dates')): ?>
<?php the_sub_field('start'); ?>
-
<?php the_sub_field('finish'); ?>
<?php endwhile; ?>
<?php the_title(); ?>
<?php endforeach; } ?>
Now one post would repeat the date field, but be sorted by the first one of them. How do I repeat the post and each have one date? Even if you can't help me with the code, I'd like to know the logic behind creating something like this.

Related

Multiple post loops in Wordpress

I've built a site for a health practitioner with CPT of:
(single-injuries.php, single-services.php, single-testimonials.php, single-partners.php)
and I have the appropriate (archive-injuries.php, archive-services, archive-testimonials, archive-partners) created with loops to display the relevant posts.
HOWEVER
I now want to create a sitemap page that pulls ALL THE POSTS from ALL ARCHIVES and just displays the PAGE NAME and URL for each...
How do I loop through multiple archives, do I nest a loop for each within a loop?
You can use a custom query that queries all of your CPTs which you listed (put them into the post_type array), similar to this (which lists all post titles found, each linked to its full post):
<?php
$args = array(
'post_type' => array('injuries', 'services', 'testimonials', 'partners' ),
'post_status' => 'publish',
);
$loop1 = new WP_Query($args);
if ( $loop1->have_posts() ) : while ( $loop1->have_posts() ) : $loop1->the_post();
$post_title = get_the_title();
?>
<div>
<p><a href='<?php echo get_the_permalink(); ?>'><?php echo post_title; ?></a></p>
</div>
<?php endwhile; else: ?>
<p>Nothing found</p>
<?php endif; ?>
I suggest you should do this using database, or what archives mean at your post? If you are using database just make a query that will select all from different tables, like this
SELECT archive-injuries.*, archive-services.*, archive-testimonials.*, archive-partners.* FROM your data base
Then make a while loop that will display posts, while mysqli_fetch_assoc have some data

ACF Relationship Field: parent conditional based on nested conditional

I have a CPT called Classes. Via an ACF Relationship field I'm allowing my client to hand pick classes to show on the front end. Each class has an expiry date.
Within a foreach statement I set up a conditional that compares the current date with the expiry date and only shows upcoming classes. What I need is to show a single note saying "there are no upcoming classes" once all the selected classes have gone past their expiry date.
ACF support suggested adding an incremental operator within the foreach loop and then checking if that value is empty. They modified my code as follows but it doesn't do the job. Additional help from ACF support falls outside the scope of what they offer, so I'm posting here for guidance. Thanks!
<?php
$all_classes = get_sub_field('class');
if( $all_classes ):
?>
<?php
$i = 0;
foreach($all_classes as $post):
setup_postdata($post);
?>
<?php
$now = time(); // get today's date
$expiry_date = strtotime(get_field('class-expiry-date')); // get the expiration date
if ($now < $expiry_date): // compare the dates and show upcoming classes only
$i++;
?>
class details
<?php endif; ?>
<?php
endforeach;
wp_reset_postdata();
?>
<?php else: ?>
<?php
//check if $i is empty
if(empty($i)):
?>
There are no upcoming classes.
<?php endif; ?>
<?php endif; ?>
Iv'e ran into this many times. My approach is a little different. Is use a normal WP_Query() and make sure the ACF relationship postobject field not saving the handpicked items as post object but as an post ID. (this is just an option to select on the custom field page). In this way I can make use of the native Wordpress meta queries
My args array is as follows allowing the query to sort on an specific date field that is equal or newer than today.
$args = array(
'post_type' => 'class', /* (your CPT slug: class or classes) */
'posts_per_page' => -1,
'meta_key' => 'class-expiry-date',
'orderby' => 'meta_value',
'order' => 'ASC',
'post__in' => array( get_sub_field( 'class' ) ),
'meta_query' => array(
array(
'key' => 'class-expiry-date',
'value' => date('Ymd', strtotime('now')),
'type' => 'date',
'compare' => '>=',
)
)
);
$wp_query = new WP_Query( $args );
if( $wp_query->have_posts() ) {
while( $wp_query->have_posts() ) {
$wp_query->the_post();
// Upcoming classes !!
// echo $post->post_title (example)
}
} else {
// There are no upcoming classes
}
wp_reset_query();
Make sure you output the custom expiry date field in the following
format Ymd (also an option on the custom fields page)

Writing a loop to conditionally display a custom post type, checking for ACF fields and Event Calendar Pro events

I'm making a site for a cookery school, and I need help with writing a loop. The loop will display the types of classes they provide, as long as each of those types of classes has upcoming events. Any help would be really appreciated, as everything I've tried so far has failed.
I'm using the Advanced Custom Fields plugin as well as the Events Calendar Pro plugin from Modern Tribe. I'll refer to the post type created by the Events Calendar Pro plugin as 'events' from now on.
I have a custom post type called 'class-type' which I've set up in my functions.php file. Each post of this type is a type of class that the cookery school offers, for instance DIY Pizza Club, Street Food Mastercless etc. The single post view will have images and details of that class, with a loop of upcoming events of that category, with the categories being the class name.
The 'class-type' post type has an ACF field called 'class_age', which is a select menu with the options 'For Adults' or 'For Kids & Teens'. It also has an ACF field called 'class_to_display', which is a taxonomy field showing categories from the Tribe Events posts.
The cookery school has two locations, and each event has it's location set to one of these.
The loop I need help with will have 4 variations on four different pages, and once I have the loop working I can make these variations myself. The variations will be:
Bristol, For Adults
Bristol, For Kids & Teens
Cardiff, For Adults
Cardiff, For Kids & Teens
So using the first variation as an example, the loop will need to do the following:
Get posts of type 'class-type'.
For each post, check that the post's ACF field 'class_age' is 'For Adults'.
Get the value in the post's ACF field 'class_to_display' and store it in a variable.
Check for posts of type 'events' with that variable as their category and with 'Bristol' as their location.
If events exist that meet those criteria, display the 'class-type' post's ACF fields of 'class_main_image', 'class_title', and 'class_short_description'.
I hope that's clear, and feel free to ask if you have any questions. Again, I really appreciate anyone taking the time to help!
Here's what I have so far which goes up to step 3 above, and is tested and working (the echo in p tags is just to check it works):
<?php
$args = array(
'post_type' => 'class-type',
'meta_key' => 'class_age',
'meta_value' => 'For Adults'
);
$the_query = new WP_Query( $args );
if( $the_query->have_posts() ):
while ( $the_query->have_posts() ) : $the_query->the_post();
$cookery_class = get_field('class_to_display'); ?>
<p><?php echo $cookery_class->name; ?></p>
<?php endwhile;
endif;
wp_reset_query(); ?>
Much to my surprise, I nailed it! Here's the code that works, hope it helps somebody.
<?php
$args = array(
'post_type' => 'class-type',
'meta_key' => 'class_age',
'meta_value' => 'For Adults'
);
$the_query = new WP_Query( $args );
if( $the_query->have_posts() ):
while ( $the_query->have_posts() ) : $the_query->the_post();
$cookery_class = get_field('class_to_display');
$cookery_class = (str_replace(' ', '-', strtolower($cookery_class->name)));
$check_has_events = tribe_get_events( array(
'tribe_events_cat' => $cookery_class,
'venue' => 59
)
);
if($check_has_events) {
$image = get_field('class_main_image'); ?>
<div class="cookery-class">
<?php if( !empty($image) ): ?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
<?php endif; ?>
<h3><?php the_title(); ?></h3>
<p><?php the_field('class_short_description'); ?></p>
</div>
<?php }
endwhile;
endif;
wp_reset_query(); ?>

Retrieve a post with its ACF repeater fields in wordpress

In a wordpress theme I am programming, I created a custom post type and its template. This custom post type displays a list thanks to advance custom fields that I attached to the post template. In fact, I used the "repeater" field of Advance custom fields pro. The user only has to insert items of the list when editing the post.
I am trying to do the following: I want to display in two specific pages templates the post with all its custom fields (the list created through the repeater). I am not able to do that, I only can retrieve the "normal" fields of the post (the title, the content...). I created a snippet so you can look at my code.
<?php //the single of the post: ?>
<ul class='slider-partners'>
<?php
//slider partners
if( have_rows('slider_partenaires_hp') ): //"slider_partenaires_hp" is the repeater field
// loop through the rows of data
while ( have_rows('slider_partenaires_hp') ) : the_row();
// display a sub field value
echo "<li class='partenaire-slide'><img src='" . get_sub_field('logo_partner') . "'></li>"; //"logo_partner" is the item inside the repeater field
endwhile;
else :
// no rows found
endif;
?>
</ul>
<?php //the template of the page where I try to retrieve the above post:
$the_query = new WP_Query(array(
'post_type' => 'our-partners',
'posts_per_page' => 1,
'order' => 'DESC'
));
while ( $the_query->have_posts() ) :
$the_query->the_post();
endwhile;
wp_reset_postdata();
?>
Could you please help me in calling a post with all its custom fields in two diferent pages ?
Thanks

How to order wordpress posts by a custom field date?

I am making an event sidebar section that will only display the next 3 events. I have got the custom post type and custom fields all working but I can seem to figure out how to order the posts by the start date of the events, which is a custom field value. Is there a php function that can compare dates and organize them into a certain order. I think it would also have to hold the post-id with the newly arranged dates so that when I read through the values, i can display the appropriate post with that date.
Does anyone have a certain direction to steer me in?
I think this is what I need to do:
Read through the posts and grab the dates
Sort the dates with the post-id associated with those dates
Read through the sorted dates and re-display the first 3 posts by post-id
I get lost on how to code that though... This is what I have so far. This code just displays them by their publish dates in wordpress.
<?php query_posts('post_type=events');
if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php $dateStart = get_post_meta($post->ID, 'date-start', true);?>
<div class="date"><?php echo $dateStart; ?></div>
<?php endwhile; endif; wp_reset_query(); ?>
I believe I was able to answer my own question. Wordpress has a built in feature to compare custom field values between posts. To compare dates, which was my custom field value, they have to be in 'yyyymmdd' format to easily be able to compare them.
I was really surprised that I didnt have to make multiple loops and store post-ids or anything. Anyways, I hope this helps someone else. :]
<?php
$args = array(
'post_type' => 'events',
'posts_per_page' => 3, //limited myself to 3 posts
'meta_key' => 'date-start', //name of custom field
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
query_posts($args);
if (have_posts()) : while (have_posts()) : the_post(); ?>
//Insert code here...
//Test to see if it is sorting the posts (below)
<?php $dateStart = get_post_meta($post->ID, 'date-start', true); echo $dateStart;?>
<?php endwhile; endif; wp_reset_query(); ?>

Categories