Home › Forums › ACF PRO › Unique value across all posts/pages › Reply To: Unique value across all posts/pages
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!!
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.