Support

Account

Home Forums ACF PRO There can only be one field checked

Solved

There can only be one field checked

  • Can someone in the forum help me to achieve this?

    I want ro have a simple checked field to feature a post, but it can only be checked once. If that field is checked and it has an older post featured it will update that older post and delete that checked field in older post.

    Something like a fixed post, bust with the tweak it can only be one fixed post.
    I think this has to be set in a function, whit something like getting that field, tell database it can only be one true between all posts and when i save the post it will update database to delete the older checked post marked as featured, not the post itself – only remove the checked or marked as featured.

    Somethink like this plugin: https://wptavern.com/there-can-only-be-one-sticky-post

  • What you need to do is create an acf/save_post filter https://www.advancedcustomfields.com/resources/acf-save_post/

    In this filter
    1) Check that it’s the right post type
    2) See if the checkbox is checked for this post
    3) If the checkbox is checked, do a query for posts that also have the checkbox checked and exclude the current post.
    4) If any posts are returned in the query then update the field uncheck it https://www.advancedcustomfields.com/resources/update_field/

    Sorry I can’t provide any code, but what looks like 4 simple steps would take me quite some time to code.

  • 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.

  • 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);
    
  • 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.

Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.