Support

Account

Home Forums ACF PRO Unique value across all posts/pages

Solved

Unique value across all posts/pages

  • I think the best use case is a concrete example. Let’s say I have a featured post on my homepage. But I don’t want to go to an Options page to configure which post is featured, I want to set this inside a post itself. I envision a True/False field called in each post and some way to ensure that when one post is featured, all of the others become “un-featured”, even if they were featured at some earlier time. I hope that’s clear.

    What is the most efficient way to accomplish this?

  • 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);
    
  • normally i do this with an option page. but i would prefer to set that post meta directly in the post too. i would do it like you but this makes me worry because of a lot of code for such a tiny thing. hmm. another approach might be to create an option field in the database which saves the post id. and toggle that id inside of each post.

  • okay. that was easy! here we go. simply adjust it to your needs:
    1. create a new entry in wp_options: “is_winner”
    2. insert that code to your functions.php:

    // register the meta box
    add_action( 'add_meta_boxes', 'my_custom_field_checkboxes' );
    function my_custom_field_checkboxes() {
        add_meta_box(
            'winner_box',          // this is HTML id of the box on edit screen
            'Winner',    // title of the box
            'my_customfield_box_content',   // function to be called to display the checkboxes, see the function below
            'product',        // on which edit screen the box should appear
            'side',      // part of page where the box should appear
            'default'      // priority of the box
        );
    }
    
    // display the metabox
    function my_customfield_box_content( $post_id ) {
        wp_nonce_field( 'wp_nonce_check', 'myplugin_nonce' );
        echo '<label><input type="checkbox" name="is_winner" value="1" /> Winner';
        if(get_the_id() == get_option("is_winner")) echo " (currently selected)";
        echo "</label>";
    }
    
    // save data from checkboxes
    add_action( 'save_post', 'my_custom_field_data' );
    function my_custom_field_data() {
        // check if this isn't an auto save
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
            return;
    
        // security check
        if ( ! isset( $_POST['myplugin_nonce'] ) || !wp_verify_nonce( $_POST['myplugin_nonce'], 'wp_nonce_check' ) )
            return;
    
        if ( isset( $_POST['is_winner'] ) )
        	update_option("is_winner", get_the_id());
        else
            return;
    }

    now in every post is a meta box along with ACF fields. when you check that meta box, the id is saved to the field in wp_options. this way, you save a lot of performance because there is no wp_query and you do not need to alter all posts!!

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

The topic ‘Unique value across all posts/pages’ is closed to new replies.