The best of the above solution is that if i have the same name field in other post types i can check them as featured and there can only be one – allways.
And the query to put the featured post in the top off all posts:
<?php
$args = [
'post_type' => ['post', 'evento'],
'meta_key' => 'post_em_destaque',
'orderby' => 'meta_value post_date',
'post_status' => 'publish',
'posts_per_page' => 6,
'paged' => $paged
];
$the_query = new WP_Query( $args );
?>
And tho show and style the post as you wish inside the loop:
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php if ( get_field( 'post_em_destaque' ) ): ?>
// The contetn of the featured post here
<?php else: ?>
// The rest of the posts
<?php endif; ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Hope this can be useful for someone in time.
The above solution was not working.
I think i found a better way of achieving what i was looking for:
function update_featured_post() {
global $posts;
global $post;
// Get other post marked as featured
$posts = get_posts([
// Array of posts to check
'post_type' => ['post', 'evento'],
'meta_key' => 'post_em_destaque',
'meta_value' => true,
'post__not_in' => [$post->ID]
]);
// Remove previous featured posts
if ( get_field( 'post_em_destaque' ) ) {
foreach( $posts as $p ) {
update_field('post_em_destaque', '0', $p->ID);
}
} return;
}
add_action('acf/save_post', 'update_featured_post', 20);
This did the job with a true or false field, please tell me if the code is very poor or if the solution is valid.
function update_featured_post( $post_id ) {
// Get current id of post being edited
$currentID = get_the_ID();
// Get all posts except current post
$posts = get_posts([
'post_type' => 'post',
'post__not_in' => [$currentID],
]);
// Get ACF true or false value
$value = get_field('post_em_destaque');
// Find if other post is marked as featured
if( $value = true ) {
foreach( $posts as $p ) {
// Uncheck field if checked
update_field('post_em_destaque', false, $p->ID);
}
}
}
add_action('acf/save_post', 'update_featured_post', 20);
Thank you for your patient, hope someone find this code useful.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.