In the post loop I have a foreach loop which displays selected team members in the admin(from the "team" custom post type) from an ACF post object.
What I want to achieve is to display the post-publish date inside the foreach.
The problem is that the code echo get_the_date('M d, Y'); shows the date of the "team" custom post type and not the blog post date because is in the foreach.
I tried to pass the post id but it's shows the posts id of the "team" post types and not the actual "post".
How can I show the 'post_type' => 'post' publish post date inside in the foreach?
<?php
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DSC'
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$postAuthors = get_field('select_post_author'); // ACF post object - custom post type - team
if( $postAuthors ): ?>
<?php foreach( array_slice($postAuthors, 0, 1) as $post): ?>
<div class="post-date">
<span>
<?php
echo get_the_date('M d, Y'); // here I want to get the post publish date but it show the publish date of the team post type items
?>
</span>
</div><!-- /.post-date -->
<?php endforeach; ?>
<?php endif; ?>
<?php endwhile;
wp_reset_postdata();
?>
Thanks
So the issue is due to $post being a global variable. I assume ACF stores an array of posts for get_field('select_post_author') then you need to change your foreach statement local variable name, e.g:
<?php foreach( array_slice($postAuthors, 0, 1) as $postAuthor): ?>
<div class="post-date">
<span>
<?php echo get_the_date('M d, Y', $postAuthor->ID); ?>
</span>
</div><!-- /.post-date -->
<?php endforeach; ?>
I solved the issue by passing the post id in the date.
<?php
$pid = get_the_ID(); //get the post id
$postAuthors = get_field('select_post_author');
if( $postAuthors ): ?>
Pass the post id in the get_the_date.
echo get_the_date('M d, Y',$pid);
Related
I have number values on my most recent post that are placed via advanced custom fields. I want to be able to pull the data from a post into another page. this can be easily done via an ID:
https://www.advancedcustomfields.com/resources/how-to-get-values-from-another-post/
but what I cannot accomplish is having this pull from the most RECENT post. ACF support site make no mention of most recent.
<?php
$args = array( 'numberposts' => '1' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
// acf query in here. not included for brevity.
endif;
}
wp_reset_query();
?>
From the Codex on https://developer.wordpress.org/reference/functions/wp_get_recent_posts/ (with slight modifications):
<?php
$recent_posts = wp_get_recent_posts(array(
'numberposts' => 1, // Number of recent posts thumbnails to display
'post_status' => 'publish' // Show only the published posts
));
foreach($recent_posts as $post) : ?>
<a href="<?php echo get_permalink($post['ID']) ?>">
<?php echo get_the_post_thumbnail($post['ID'], 'full'); ?>
<p class="custom-class"><?php echo $post['post_title'] ?></p>
</a>
<?php
$custom_field = get_field('custom_field_name', $post['ID']);//for ACF fields
?>
<?php endforeach; wp_reset_query(); ?>
I'm trying to use get_field to return a simple text field and it returns empty for some reason. A field itself is where it should be and there is text in it, so that part is all set. This PHP code is loaded via php snippet, post thumbnail for example, shows up perfectly. So everything works except for ACF field value.
<div class="your-class">
<?php
$args = array(
'post_type' => 'home_test',
'posts_per_page' => -1,
'orderby' => 'name',
'order' => 'ASC',
);
$the_query = new WP_Query($args);
$brand = get_posts($args);
foreach ($brand as $post) {
setup_postdata($post);
$thumbnail = get_the_post_thumbnail_url($post->ID, 'full');
$homelinkvalue = get_field("home_brand_link");
if (!$thumbnail)
continue;
?>
<div>
<p><?php echo $homelinkvalue; ?></p><img src="<?php echo $thumbnail; ?>">
</div>
<?php
}
wp_reset_postdata();
?>
</div>
I think the issue is that you are mixing together a custom post loop (your foreach and setup_postdata()) but then are using functions like get_field(), which make use of the global post object. In this case, get_field() tries to lookup the field value by checking against the global $post, but it has not been properly set. See warning here about setup_postdata($post):
You must pass a reference to the global $post variable, otherwise functions like the_title() don't work properly.
You can implement this in your code with a slight modification:
global $post;
foreach ($brand as $currPost) {
$post = $currPost;
setup_postdata($post);
// Rest of code as normal
}
Or, since get_field() can accept a specific post as an argument instead of automatically using the global one, you could change:
$homelinkvalue = get_field("home_brand_link");
to:
$homelinkvalue = get_field("home_brand_link",$post->ID);
Side note: Normally, the recommended way to iterate posts is with the special "WP loop" pattern, something like:
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<!-- Do something -->
<?php endwhile; ?>
Using the above pattern automatically sets the global $post variable as it loops through, which allows devs to use functions like get_field() without having to worry about explicitly passing in a specific post; makes things a bit easier.
Try this one:
<div class="your-class">
<?php
$args = array(
'post_type' => 'home_test',
'posts_per_page' => -1,
'orderby' => 'name',
'order' => 'ASC',
);
$the_query = new WP_Query( $args );
if ($the_query->have_posts) :
while($the_query->have_posts) : $the_query->the_post();
?>
<div>
<p><?php the_field( "home_brand_link" ); ?></p>
<img src="<?php the_post_thumbnail_url(); ?>">
</div>
<?php
endwhile;
wp_reset_postdata();
endif;
?>
</div>
In wordpress, using the CTU plugin, I made a custom post type called apartments with a field called available. I only want to target any post with available=yes. How would I do this? I tried the_post('available=yes')
<?php
$args = array(
'post_type' => 'apartment'
);
$the_query = new WP_Query( $args );
?>
<?php if( have_posts()): ?>
<?php while ($the_query->have_posts()): ?>
<?php $the_query->the_post(); ?>
<?php $the_field('available'); ?>
<?php endwhile; ?>
<?php else: ?>
<?php endif; ?>
i think the simplest solution is:
1. In your custom taxonomy create a term called available
2. add this to your custom query
$args = array(
'post_type' => 'apartment',
'category_name' => 'available'
);
I have two CPTs, one called 'artist' and the other called 'release.' I've created a single-artist.php page that displays an artist and its' custom meta data. On that same page I am displaying all releases by that artist with the following code:
<!-- GET RELEASES -->
<?php
$category = get_the_category();
$artist_name_slug = $category[0]->slug;
$args = array ('post_type' => 'release', 'posts_per_page' => 20, 'category_name' => $artist_name_slug);
query_posts ($args);
?>
<?php if (have_posts()) : ?>
<h3 class="artist-col2-title">Releases</h3>
<?php while (have_posts()) : the_post(); ?>
<div class="artist-release"><?php echo the_post_thumbnail('small'); ?></div>
<?php endwhile; ?>
<?php endif; ?>
<div style="clear:both;"></div>
Within the release CPT I have a release date in the meta data.
I would like to sort the releases based on that date but I cannot figure out how to add that to my arguments. Any help would be greatly appreciated!
To sort by meta data you can use
$args = array (
'post_type' => 'release',
'posts_per_page' => 20,
'category_name' => $artist_name_slug,
'meta_key' => 'your_meta_key' // i.e. release_date
'orderby'='meta_value' // for numeric value use 'meta_value_num' instead
);
query_posts ($args);
But notice the meta_key should be present in the query that you want to use for sorting, see for more.
I am trying to figure out how to sort the search results by post type. I asked the question on wordpress stack exchange and someone tried answering but I don't think they understood what I was trying to achieve.
-- https://wordpress.stackexchange.com/questions/72914/search-results-sorted-by-post-types
For example, when a person searches a term they are taking to the page
with the results, all >posts found from all post types are shown but
up top there are the different post types name >links that will sort
the results and show only the respective post type's post. Right now I
have several loops on the search results page for each post type but when I test it, the >loops are all showing the same results even
though each loop has a query for a different post type.
My code for the search page - http://pastebin.com/L9zEw1cn
This is a little above the first loop <?php global $wp_query; $total_results = $wp_query->found_posts; ?> This is the first loop <?php if(have_posts()) : ?> <?php while(have_posts()) : the_post() ?> //My divs// <?php endwhile; endif; ?> <?php wp_reset_postdata(); ?>
Then the second loop which is the same for all the others except the post type name -- <?php $args = array( 'post_type' => 'videos', 's' => $s ); ?> <?php if(have_posts()) : ?> <?php while(have_posts()) : the_post() ?> //My divs <?php endwhile; endif; ?> <?php wp_reset_postdata(); ?>
How can I fix it so that each loops display the search results for only that post type?
Try using get_posts for the second loop.
$args = array(
'post_type'=> 'videos',
'numberposts' => -1,
's' => $s
);
$videos = get_posts( $args );
foreach( $videos as $post ) : setup_postdata($post);
// My divs
endforeach;
http://codex.wordpress.org/Template_Tags/get_posts
Edit: added 'numberposts' to the arguments.