Post Objects in Advanced Custom Fields order by date - php

Hi I have a Post Object field in Advanced Custom Fields that I want to return multiple posts, ordered by date. I have the custom field data from those posts returning fine, but the Post Objects return in order of the Post ID. I want them to be ordered by the date that the post was published.
<?php $post_objects = get_field('exhibitions');
if( $post_objects ): ?>
<?php foreach( $post_objects as $post_object): ?>
<a href="<?php echo get_permalink($post_object->ID); ?>">
<div style="display: inline-block">
<? if( get_field( 'title', $post_object->ID) ): ?>
<em><?php the_field('title', $post_object->ID); ?></em><br>
<?php endif; ?>
<? if( get_field( 'dates', $post_object->ID) ): ?>
<?php the_field('dates', $post_object->ID); ?>
<?php endif; ?>
</div>
</a>
<br><br>
<?php endforeach; ?>
<?php endif; ?>
This returns the text custom fields 'title' and 'dates' from each post thats selected in the Post Objects field on the post where this is called.
I want the posts to return here by order of their publish date.
Any ideas?

#Michael Ray-Von - your answer worked, but it involved getting the same data from the db twice. Instead you can just sort the post data returned in your initial ACF query rather than running the extra query. (The post_date is returned as a string so you can strcmp it):
<?php
// get the posts from ACF
$custom_posts = get_field('your_posts_field');
// sort the posts by post date, but you can also sort on ID or whatever
usort($custom_posts, function($a, $b) {
return strcmp($b->post_date,$a->post_date);
});
// write them out
foreach ($custom_posts as $post) : setup_postdata($post); ?>
<article>
<h1><?php the_title();?></h1>
<?php the_excerpt(); ?>
</article>
<?php
endforeach;
wp_reset_query();
?>
Hat-tip to this answer for the sorting: https://stackoverflow.com/a/10159521

Okay i've got it figured out!
Instead of calling get_field as the post_objects, you call it as a variable just to get the IDs of relevant posts, and then use that in an array for the $args of a get_posts. That way you have access to all the array options of get_posts before running the loop.
<?php
$ids = get_field('exhibitions', false, false);
$args = array(
'post__in' => $ids,
'orderby' => 'post_date',
);
$post_objects = get_posts( $args );
if( $post_objects ): ?>
<?php foreach( $post_objects as $post_object): ?>
<a href="<?php echo get_permalink($post_object->ID); ?>">
<div style="display: inline-block">
<? if( get_field( 'title', $post_object->ID) ): ?>
<em><?php the_field('title', $post_object->ID); ?></em><br>
<?php endif; ?>
<? if( get_field( 'dates', $post_object->ID) ): ?>
<?php the_field('dates', $post_object->ID); ?>
<?php endif; ?>
</div>
</a>
<br><br>
<?php endforeach; ?>
<?php endif; ?>
Thanks for your help!
found my answer thanks to: http://support.advancedcustomfields.com/discussion/5846/adding-args-to-post_objects-get_field/p1

<?php $post_objects = get_field('exhibitions');
$args = array (
'orderby'=>'date',
);
$the_query = new WP_Query( $args );
if($the_query->have_posts()) {
while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a href="<?php echo get_permalink($post_object->ID); ?>">
<div style="display: inline-block">
<? if( get_field( 'title', $post_object->ID) ): ?>
<em><?php the_field('title', $post_object->ID); ?></em><br>
<?php endif; ?>
<? if( get_field( 'dates', $post_object->ID) ): ?>
<?php the_field('dates', $post_object->ID); ?>
<?php endif; ?>` </div>
</a>
<br><br>
endwhile;
wp_reset_postdata();
}
I haven't tested, But it should work for you with little adaption !

Related

Nested foreach loop not functioning correctly. - wp

I have custom post types 'stockist' and 'product', the latter having a taxonomy 'range'. Products are assigned to stockists using an ACF Post Object.
I'm trying to loop through 'stockist', return the 'range' term from the post object and then list the products for each term. So far I can loop all stockists and return all range terms however the foreach loop that returns the product list doesn't appear to be looping as it only returns the first set of values for each stockist.
Can anyone see where I'm going wrong?
<?php
$args2 = array( 'post_type' => 'stockist', 'posts_per_page' => -1 );
$stockistloop2 = new WP_Query( $args2 );
if ( $stockistloop2->have_posts() ): while ( $stockistloop2->have_posts() ): $stockistloop2->the_post();?>
<div class="col-1-1 clearfix nopad stockist-block-dropdown <?php the_title();?>-block">
<?php
$args = array( 'taxonomy' => 'range', 'hide_empty' => 0);
$categories = get_categories($args);
if($categories): foreach($categories as $category): $url = get_category_link( $category->term_id ); ?>
<div class="col-1-5">
<h4>
<?php echo ($category->name) ;?>
</h4>
<ul class="stockist-block-products clearfix">
<?php $post_objects = get_field('stocked_range'); if( $post_objects ):
foreach( $post_objects as $post): $post_terms_array = get_the_terms($post, 'range'); $post_term_name = $post_terms_array[0]->slug;
setup_postdata($post);
if($post_term_name == $category->slug):?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php endif;
endforeach;
endif;?>
</ul>
</div>
<?php endforeach; ?>
<?php endif;?>
</div>
<?php endwhile; wp_reset_postdata(); endif; ?>
UPDATE
Replacing the inner loop with the following has resolved the issue:
<?php $post_objects = get_field('stocked_range');
if( $post_objects ): ?>
<ul class="stockist-block-products clearfix">
<?php foreach( $post_objects as $post_object): $post_terms_array = get_the_terms($post_object, 'range'); $post_term_name = $post_terms_array[0]->slug;
if($post_term_name == $category->slug):?>
<li>
<?php echo get_the_title($post_object->ID); ?>
<span>Post Object Custom Field: <?php the_field('field_name', $post_object->ID); ?></span>
</li>
<?php endif; endforeach; ?>
</ul>
<?php endif;?>

Advance Custom fields keep returning the same value

So to elaborate, I have 11 posts and in each of those posts I input an image into advance custom fields form. But when I call them I get back 11 results but from just 1 post.
Here is what I'm working with. Just to tell you this is in functiuons.php since I want to get this as a shortcode so I can use it on multiple post types.
function get_slider() {
$args = array(
'post_type' => 'projekti',
'posts_per_page' => -1,
);
$posts = get_posts($args);
ob_start();
if( $posts ): ?>
<div class="slider_slick">
<?php foreach($posts as $post): setup_postdata( $post ); ?>
<?php if( have_rows('slider') ): ?>
<?php while( have_rows('slider') ): the_row(); ?>
<?php // vars
$image = get_sub_field('image_slider');
$link = get_sub_field('slider_link');
?>
<div class="slide">
<a href="<?php echo $link; ?>">
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" />
</a>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
</div>
<?php endif;
return ob_get_clean();
}
add_shortcode ('slick_slider' , 'get_slider');
I have almost identical code on the template I created, but that one works, this one does not and I don't know why.
Pass id of current post for the acf have_rows() function. I have done that using , $currentId = get_the_ID(); and then using the variable $currentId where I need.
function get_slider() {
$args = array(
'post_type' => 'projekti',
'posts_per_page' => -1,
);
$posts = get_posts($args);
ob_start();
if( $posts ): ?>
<div class="slider_slick">
<?php foreach($posts as $post): setup_postdata( $post );
$currentId = get_the_ID();
?>
<?php if( have_rows('slider', $currentId) ): ?>
<?php while( have_rows('slider', $currentId) ): the_row(); ?>
<?php // vars
$image = get_sub_field('image_slider');
$link = get_sub_field('slider_link');
?>
<div class="slide">
<a href="<?php echo $link; ?>">
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" />
</a>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
</div>
<?php endif;
return ob_get_clean();
}
add_shortcode ('slick_slider' , 'get_slider');
I had to add
global $post;
to the top of my function for it to work. Thanks for your help anyway guys.

Advanced Custom Fields - Relationship Field

I am using the relationship field within a custom post type, but my content is not showing up. I have doubled checked against the documentation but I cant see why it wouldn't be working.
Any help would be great!
Documentation here: http://www.advancedcustomfields.com/resources/relationship/
My html is:
<?php
// The Query
$args = array(
'post_type' => 'top-car'
);
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php
$posts = get_field('top-cars');
if( $posts ): ?>
<ul id="recom">
<?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
<?php setup_postdata($post); ?>
<li>
<div class="recom-single">
<h2><?php the_field( 'model' ); ?></h2>
</div>
<!--Close Recom Single-->
</li>
<!--Close First Car-->
<?php endforeach; ?>
</ul>
<!--Close Slider-->
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
<?php endwhile; else: ?>
<p><?php _e('Sorry, we have no cars available at this time, please contact us.'); ?></p>
<?php endif; ?>

Can't reset query to show ramdom post after firs loop Wordpress

I have a piece of code to show a random post under a post. But this code only show me the post's write from the author of that post. Whats wrong?
<!-- post -->
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?>
<?php echo get_avatar( get_the_author_meta( 'ID' ), 32 ); ?>
<?php the_author(); ?>
<?php the_category(none); ?>
<?php the_date(); ?>
<?php the_content();?>
<?php $key="video"; echo get_post_meta($post->ID, $key, true); ?>
<?php $key="imagen"; echo get_post_meta($post->ID, $key, true); ?>
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
<!-- ramdom post -->
<?php $posts = $posts = get_posts('orderby=rand&numberposts=3'); foreach($posts as $post) { ?>
<?php the_post_thumbnail('photo-thumbnail') ?>
<?php the_author(); ?>
<?php the_category(none); ?>
<?php } ?>
Try adding <?php wp_reset_query(); ?> after the first query.
Replace the random post code with the code below. In your example you were querying 3 posts. If that's not your intention and you only want a single post replace 3 with 1 in posts_per_page.
$secondary_posts = new WP_Query( array( 'orderby' => 'rand', 'posts_per_page' => 3, 'no_found_rows' => 1, ) );
if ( $secondary_posts->have_posts() ) : while ( secondary_posts->have_posts() ) : $secondary_posts->the_post();
the_post_thumbnail( 'photo-thumbnail' );
the_author();
the_category();
endwhile; endif;
wp_reset_postdata();

Date Base Archive On WordPress Homepage

Sorry, I am pretty new to WordPress coding and learning it from scratch. Please pardon me for my lack of experience.
I have a custom post type called 'Products' and want to display only them as a date based archive in homepage. But, I Don't want to display the post titles or any other content from the posts except the featured from first three or four posts in that date. Something like this:
I am trying the following code, but it returns the posts as the normal loop.
<?php $query = new WP_Query( array('post_type' => 'products', 'orderby' => 'date') ); ?>
<?php if ( $query->have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php
$archive_year = get_the_time('Y');
$archive_month = get_the_time('m');
$archive_day = get_the_time('d');
?>
<div class="idpostdate">
<?php the_date( 'F j, Y', '', '<span class="datetext">: Click Here To View Products</span>', true );?>
</div>
<div class="thumbnail">
<?php if ( has_post_thumbnail() ) { the_post_thumbnail('homet'); } ?>
</article>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'no-results', 'index' ); ?>
<?php endif; ?>
Any guidance?
Functions like the_ID() and the_post_thumbnail() run on the main query. If you want to use their equivalents in your code, you'll need to prepend $query-> to them.
Untested, but I think it'll do what you want it to:
<?php $query = new WP_Query( array('post_type' => 'products', 'orderby' => 'date') ); ?>
<?php if ( $query->have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<article id="post-<?php $query->the_ID(); ?>" <?php $query->post_class(); ?>>
<?php
$archive_year = $query->get_the_time('Y');
$archive_month = $query->get_the_time('m');
$archive_day = $query->get_the_time('d');
?>
<div class="idpostdate">
<?php $query->the_date( 'F j, Y', '', '<span class="datetext">: Click Here To View Products</span>', true );?>
</div>
<div class="thumbnail">
<?php if ( $query->has_post_thumbnail() ) { $query->the_post_thumbnail('homet'); } ?>
</article>
<?php endwhile; ?>
<?php else : ?>
<?php get_template_part( 'no-results', 'index' ); ?>
<?php endif; ?>

Categories