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(); ?>
Related
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(); ?>
I need to render all the posted "custom post type content" content from a "xx" user.
I have been trying to accomplish this, but of course, something is wrong.
that's why Im here, to learn how to make things right.
I have tried with this code: works. shows all the posts, but not from the author. How can I define wich author's post should be queried?
I can render the custom post types but I need to query only the author. Im making some sort of customized "archive" page.
<?php
query_posts(array(
'post_type' => 'video_listing',
'showposts' => 10
) );
?>
<?php while (have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<p><?php echo get_the_excerpt(); ?></p>
<?php endwhile;?>
You should be able to extend your query_posts() function call with the author or author_name parameter.
query_posts(array(
'post_type' => 'video_listing',
'author' => 1
'showposts' => 10
));
According to the example above 1 is the ID of the author you like to filter for.
I just started using advanced custom fields and it is awesome. I have been struggling with this one issues and I would love some help with it.
I have setup a product name, price and image field under the products field group. I have it working so it shows the information on the product page but I have no clue how to do the loop for the overview of all products (i.e. I want to make a page that has all the products on it).
<?php if(get_field('products')): ?>
<?php the_sub_field('product-name');?>
<?php the_sub_field('product-price');?>
<?php the_sub_field('product-image');?>
<?php endif; ?>
Is anyone on the interweb able to help me out and give me a quick lesson on advanced custom fields looping? I would appreciate it very much.
I'm assuming this is a field for a custom post type?
If so, you just need to do a WordPress loop.
<?php
// Loop through custom post type
$loop = new WP_Query( array( 'post_type' => 'YOURPOSTTYPE', 'posts_per_page' => -1, 'orderby' => 'menu_order', 'order' => 'ASC' ) );
while ( $loop->have_posts() ) : $loop->the_post();
if(get_field('products')) :
the_sub_field('product-name');
the_sub_field('product-price');
the_sub_field('product-image');
endif;
endwhile;
?>
So I have a custom post type called "Events". In each event, i have a custom field called "start_time_date" which returns a date like "08/01/12". How would i sort these posts to return the posts so that the posts are sorted by the start_time_date in an ascending order (start_time_dates closer to current date gets shown first).
Here's my current code:
<? query_posts("cat=$currentID&showposts=100"); ?>
<? if ( have_posts() ) : ?>
<? while ( have_posts() ) : the_post(); if (time() < strtotime(get('end_time_date'))) :?>
<? $originalDate = get('start_time_date'); $newDate = date("M j, Y", strtotime($originalDate)); ?>
<div class="post">Some Post Data</div>
<? endif; endwhile; ?>
<? endif; ?>
Did you try something like:
<? query_posts("cat=$currentID&showposts=100&orderby=date&order=asc"); ?>
I am not sure what exactly date is in the database columns but it's something like that. Also, the order might need to but changed but I don't think it matters.
Edit
I did some more research and &orderby=date&order=asc is correct for the date and ordering.
Sorry about that, did you read through Displaying Posts Using a Custom Select Query in the WordPress codex?
They also added query_posts('&meta_key=popularity&orderby=meta_value'); where meta_key is the name of your custom field.
Found here: Class Reference/WP Query
Use a meta field that stores a date in a mysql format i.e. 2012-08-02 for that post. Maybe consider using the jquery date picker to render this format for you.
This post when saved should now have a row in the the wp_postmeta table.
$results = new WP_Query( array( 'meta_key' => 'date', 'orderby'=> 'meta_value' ));
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.