Get values from most recent post - wordpress

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(); ?>

Related

How can I show all posts of a custom post type from current single post category in WordPress?

I have a custom post type and it have many categories. When I open a single post of this custom post type post, then at the bottom of this custom single post, I want to show 8 posts title, image etc from this single post category.
Add this code in single.php of your theme.
$related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID), 'numberposts' => 8, 'post__not_in' => array($post->ID) ) );
if( $related ) foreach( $related as $post ) {
setup_postdata($post); ?>
<ul>
<li>
<?php the_title(); ?>
<?php the_content('Read the rest of this entry ยป'); ?>
<?php the_post_thumbnail( 'thumbnail' ); ?>
</li>
</ul>
<?php }
wp_reset_postdata(); ?>

Advanced Custom Fields and Google Maps Multiple Markers

I have some code that generates a Google Map to show a marker based on the location field supplied via a custom field named 'event_map'. It works great.
<?php
$location = get_field('event_map');
if( !empty($location) ):
?>
<div class="acf-map">
<div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>"></div>
</div>
<?php endif; ?>
I now want to add a map that shows ALL of the markers from ALL of the posts from the 'tour-dates' custom post type. How would I go about doing this assuming all of these posts have a field called 'event_map' to draw location data from? I am stumped and any help appreciated.
Get the posts with such post type and loop on it then use the ACF API get_field (the second parameter let us define the post ID in which your custom field value belongs to).
<?php
$tours = get_posts( array(
'post_type' => 'tour-dates',
'posts_per_page' => -1
));
if( !empty($tours) ): ?>
<div class="acf-map">
<?php foreach($tours as $tour): ?>
<?php
$location = get_field('event_map',$tour->ID);
if( !empty($location) ): ?>
<div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>"></div>
<?php endif; ?>
<?php endforeach; ?>
</div>
<?php endif; ?>
This query should get all tour-dates posts that have information in the event_map field.
$new_loop = new WP_Query( array(
'post_type' => 'tour-dates',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'event_map',
'compare' => 'EXISTS'
)
)
) );
I'm not sure what you're using to display the map, so I can't help beyond that.

Target custom field type plugin value using WP_Query

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'
);

Displaying Custom Category loop in homepage

Thing is, In the homepage of my theme, I want to show post from different category in Different Div. Each DIV will contain 3 post from a category. I need a loop that can pick last 3 post from a specific Category. Can't find any suitable ans for it.
To explain things more easily, here is a demo picture of the Content section,
http://i.imgur.com/5QSzAIS.png
It will be a great help, if someone help me with the code !
<?php query_posts('cat=10&posts_per_page=3'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
This should get you started. You need to use this code twice. Where it says cat=10, you should enter your category ID (you can check this when you click on a Category from the admin panel, the the browser it will show something like this http://yourwebsite.com/wp-admin/edit-tags.php?action=edit&taxonomy=category&tag_ID=4&post_type=post)
Where it says tag_ID is the ID of your category.
I'm currently using a different method on a page of the site I'm building which allows me to run multiple loops in one page and specify the category for each one. This method I personally like better as it is more straightforward to me, and I can define the category with the slug instead of the ID.
Instead of using have_posts() and such, you use WP_Query() after defining your array and then wp_reset_postdata() to end your loop. The benefit is that you can keep running loops this way.
I'm also loading the data from custom fields in my posts using get_post_meta, but this method will work without that stuff.
<div class="audioGrid">
<?php
$args = array( 'post_type' => 'post',
'category_name' => 'audio',
'posts_per_page' => 3,
'order' => 'DESC' );
$query1 = new WP_Query($args);
while ( $query1->have_posts() ) {
$query1->the_post();
?>
<div id="<?php echo( basename(get_permalink()) ); ?>" class="grid_item">
<?php the_post_thumbnail( 'audio-thumb' ); ?>
<h3><?php the_title(); ?></h3>
<p><?php echo get_post_meta($post->ID, 'post_description', true); ?></p>
<a target="blank" href="<?php echo get_post_meta($post->ID, 'audio_link', true); ?>"></a>
</div>
<?php the_content(); ?>
<?php } ?>
</div> <?php // end Audio Grid ?>
<?php wp_reset_postdata(); ?>
<div class="videoGrid">
<?php
$args2 = array( 'post_type' => 'post',
'category_name' => 'video',
'posts_per_page' => 3,
'order' => 'DESC' );
$query2 = new WP_Query($args2);
while ( $query2->have_posts() ) {
$query2->the_post();
?>
<div id="<?php echo( basename(get_permalink()) ); ?>" class="grid_item">
<?php the_post_thumbnail( 'video-thumb' ); ?>
<h3><?php the_title(); ?></h3>
<p><?php echo get_post_meta($post->ID, 'post_description', true); ?></p>
<a target="blank" href="<?php echo get_post_meta($post->ID, 'video_link', true); ?>"></a>
</div>
<?php the_content(); ?>
<?php } ?>
</div> <?php // end Video Grid ?>
<?php wp_reset_postdata(); ?>
Another cool thing I'm doing is using a custom field to define the order of things and using meta_key and meta_value_num to get that number and force the order how I want, and since this site isn't complicated, defining the order this way is convenient. I just use leading zeroes to make it easy: 001, 002, 003, etc
<?php
$args2 = array( 'post_type' => 'post',
'category_name' => 'video',
'posts_per_page' => 3,
'meta_key' => 'video_order',
'orderby' => 'meta_value_num',
'order' => 'ASC' );
$query2 = new WP_Query($args2);
while ( $query2->have_posts() ) {
$query2->the_post();
?>
Anyway, hope this helps if you need to use multiple loops to pull posts from different categories.

Wordpress - Grab Item by Category in Post Type Query

I have setup a custom post type in Wordpress. It is a section where the client can upload documents in PDF format. The documents are divided into two categories - 'Downloadable Forms' and 'Menu' ; the Custom Field name for the category is 'document_category'
I am trying to run a query and only display the 'Downloadable Forms' on a page. Here is the code I normally use --- I'm hoping someone can help me add what I need to make it work?
<?php
$args = array('post_type' => 'prep_forms', 'posts_per_page' => -1);
// The Query
$the_query = new WP_Query( $args );
// The Loop
$i = 0;
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<li><?php the_title(); ?></li>
<?php
$i++;
endwhile;
// Reset Post Data
wp_reset_postdata();
?>
Thanks.
<?php
$args = array('post_type' => 'prep_forms', 'posts_per_page' => -1);
// The Query
$the_query = new WP_Query( $args );
// The Loop
$i = 0;
while ( $the_query->have_posts() ) :
$the_query->the_post();
// Get all Advanced custom fields
$my_custom_fields = get_fields(get_the_ID());
// Does document_category field exists and is it equal with 'Downloadable Forms'?
if(isset($my_custom_fields['document_category']) && $my_custom_fields['document_category'] == 'Downloadable Forms'):
?>
<li><?php the_title(); ?></li>
<?php
endif;
$i++;
endwhile;
// Reset Post Data
wp_reset_postdata();
?>
I've used the get_fields($post_id = false) function of the Advanced Custom Fields plugin that returns an array of all the posts' custom fields and then filtered it to match your needs.
Hope i've helped
Well, you can use the is_category(); inside loop and only display those posts, like so (using your same code):
<?php
$args = array('post_type' => 'prep_forms', 'posts_per_page' => -1);
// The Query
$the_query = new WP_Query( $args );
// The Loop
$i = 0;
while ( $the_query->have_posts() ) : $the_query->the_post();
if(is_category('Sownloadable Forms')){ // here the condition
?>
<li><?php the_title(); ?></li>
<?php
$i++;
} // here ends the IF statment
endwhile;
// Reset Post Data
wp_reset_postdata();
?>
Better yet: http://codex.wordpress.org/Function_Reference/is_category
Hope that helps.
Why dont you make it simpler and set a query based on custom field data?
<?php
$count = 1;
$args = array(
'post_type' => 'any',
'posts_per_page' => 4,
'meta_key' => 'display',
'meta_value' => 'true'
);
$query = new WP_Query();$query->query($args);
while ($query->have_posts()) : $query->the_post();
$do_not_duplicate[] = $post->ID;
?>
<!-- query content -->
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php printf( esc_attr__( 'Permalink to %s', 'advanced' ), the_title_attribute( 'echo=0' ) ); ?>" ></h2>
<?php the_excerpt() ;?>
<!-- query content -->
<?php $count++; endwhile; wp_reset_query(); ?>
That way any post with custom field key display and custom field value true will be shown on your query.

Resources