Support

Account

Home Forums ACF PRO Unique value across all posts/pages Reply To: Unique value across all posts/pages

  • I decided to give this a shot and also decided it would be best to allow for configuring the Featured post from within the post itself or from my site’s home page (i.e., front page). I also wanted to ensure this would work for scheduled posts. I’d love feedback on more effective methods or if any of this code can be tightened up at all.

    My custom post type is article and I setup two fields: a checkbox within each article and a Post Object field on the home page, both fields called featured_article. Here’s how I did it:

    
    function allow_only_one_featured_article($post_id, $post) {
      if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
      }
    
      $homepage = get_option('page_on_front');
    
      if($homepage == $post_id && get_post_status(get_post_meta($post_id, 'featured_article', true)) == 'publish') {
        // only fires during save_post hook if Home was just saved AND the chosen Article is published
    
        $exclude = get_post_meta($post_id, 'featured_article', true);  // save the Article ID to exclude from the query (below) that removes Featured status from other Articles
        update_post_meta($exclude, 'featured_article', 1);             // check the Featured Article's checkbox
    
      } elseif(get_post_type($post_id) == 'article' && (get_post_meta($post_id, 'featured_article', true) == 1 || get_post_meta($homepage, 'featured_article', true) == $post_id)) {
        // if an Article has either its Featured checkbox is checked, or it's selected on Home
    
        $exclude = $post_id;                                           // save the Article ID to exclude from the query (below) that removes Featured status from other Articles
        update_post_meta($exclude, 'featured_article', 1);             // check Article's checkbox (in case scheduled Article is set from Home)
        update_post_meta($homepage, 'featured_article', $post_id);     // update Home selection
    
        if($post->post_status != 'publish') {                          // don't remove Featured status from other Articles unless the Article is published
          return;
        }
    
      } else {
        return;
      }
    
      // Unset all Featured Articles except the current one that's being saved or published
      $featured_articles = new WP_Query(array(
        'post_type'    => 'article',
        'post__not_in' => array($exclude),
        'meta_query'   => array(array(
          'key'        => 'featured_article',
          'value'      => 1,
          'compare'    => '='
        )),
        'fields'       => 'ids'
      ));
      $featured_article_ids = $featured_articles->posts;
    
      foreach($featured_article_ids as $f) {
        update_post_meta($f, 'featured_article', 0);
      }
    
    }
    add_action('save_post', 'allow_only_one_featured_article', 10, 2);
    add_action('publish_article', 'allow_only_one_featured_article', 10, 2);