Home › Forums › Backend Issues (wp-admin) › True/false – When new true, update the rest to false
Hi there,
I was trying to accomplish something with a true/false field: Lets say that I have a magazine, feeding the articles in a tile-layout; I need a featured article that will be shown in different style; Only it is possible to have a featured item.
To do it I have a ‘true/false’ custom field that reads “Featured article”.
Now the question is: When I select a new featured article, how do I update all the rest to false?
Hi that’s exactly what i had to do for a project, mine was about shoes but that’s how i set it up:
When you Publish a post if the Featured checkbox on the post is set to true
the previous Featured post is set to false
so you always have a single featured post.
basically that post runs on every acf/save_post
and runs before the data are saved.
The code check if you set the true/false option for the post you are saving.
if you did it makes a query (in my example restricted by post type) to see if there is any featured posts. If there is it will set the featured post value to false, if there isn’t it will do nothing, in both cases it will end up setting the featured value of the saved post to true.
//First you would have to add acf/save_post action to you function.php so the action runs every time a post is saved or udpated
// run before ACF saves the $_POST['fields'] data
add_action('acf/save_post', 'my_acf_save_post', 1);
function my_acf_save_post( $post_id ){
// vars
$fields = false;
//here i added a if stattement so the code runs only for a defined post //type you can change "pompitup_product" or delet that if statement
if(get_post_type( $post_id ) === "pompitup_product"){
//check if Fields have been posted
if( isset($_POST['fields']) ){
//store the true/false field on a variable (change
//the "Field_xxx" id by yours)
$feature_product = $fields[field_52b2c5c9d8267];
//check if the true/false field is true
if( $feature_product ){
//if it's set to true do a WP_query so you can get
//the other post that have $feature_product set to true
//here you may want to change the 'post type' to
//whatever your is (or to all)
//in the meta query you sholud use your
// true/false field name as a key
$args = array( 'post_type' => 'pompitup_product',
'meta_query' => array(
array(
'key' => 'produit_mis_en_avant',
'value' => '1',
'compare' => '==')),);
$post_query = new WP_Query($args);
//here we check if the query returns post (if it does
//it will return featured posts
if($post_query->have_posts() ) {
while($post_query->have_posts() ) {
//here we get the featured post id
$post_query->the_post();
//for each posts we update the true/false
//field and set it to false
//again use your own field_xxx here
update_field('field_52b2c5c9d8267', false, get_the_ID());
}
}
}
}//end isset($_POST['fields'])---->for post
}//end check if post-type== pompitup_product
}//end my_acf_save_post
sorry about the bad indentation, I find it really hard to past code in there.
I think there is a cleaner way to do that but that gets it working.
Hey thanks,
clearly explained and well commented! I’ll let you know how it goes 🙂
Ummm something doesn’t go well. Here is my code:
/* When a ACF "Featured article" (in 71% articles) is set to true, all the rest are updated to false */
/* First you would have to add acf/save_post action to you function.php so the action runs every time a post is saved or udpated run before ACF saves the $_POST['fields'] data */
add_action('acf/save_post', 'my_acf_save_post', 1);
function my_acf_save_post( $post_id ){
$fields = false;
$fieldId = 'field_5343d06c3e9dc';
$catId = 27;
/* Here I added a if statement so the code runs only for a defined category */
if ( in_category( $catId, $post_id ) ) {
// Check if Fields have been posted
if ( isset( $_POST['fields'] ) ) {
// Store the true/false field on a variable (change the "Field_xxx" id by yours)
$featuredArticle = $fields[$fieldId];
// Check if the true/false field is true
if ( $featuredArticle ) {
/* If it's set to true do a WP_query so you can get the other post that have $featuredArticle set to true here you may want to change the 'cat' to whatever your is (or to all) in the meta query you should use your true/false field name as a key */
$args = array(
'cat' => $catId,
'meta_query' => array(
array(
'key' => 'featured_article',
'value' => '1',
'compare' => '=='
)
)
);
$post_query = new WP_Query( $args );
/* Here we check if the query returns post (if it does it will return featured posts) */
if ( $post_query->have_posts() ) {
while ( $post_query->have_posts() ) {
$post_query->the_post();
/* For each posts we update the true/false field and set it to false */
update_field( $fieldId, false, get_the_ID() );
}
}
}
}
}
} // end my_acf_save_post
I had a typo on my example code as i had to delete some necessary stuff.
you should add $fields = $_POST['fields'];
after if( isset($_POST['fields']) ){
and before $featuredArticle = $fields[$fieldId];
let me know
Wonderful! You saved me a good headache!
Here is my/your final code, hope someone finds it useful!
/* When a ACF "Featured article" (in 71% articles) is set to true, all the rest are updated to false */
/* First you would have to add acf/save_post action to you function.php so the action runs every time a post is saved or udpated run before ACF saves the $_POST['fields'] data */
add_action('acf/save_post', 'my_acf_save_post', 1);
function my_acf_save_post( $post_id ){
$fields = false;
$catId = 27;
/* Here I added a if statement so the code runs only for a defined category */
if ( in_category( $catId, $post_id ) ) {
// Check if Fields have been posted
if ( isset( $_POST['fields'] ) ) {
$fieldId = 'field_5343d06c3e9dc';
$fields = $_POST['fields'];
// Store the true/false field on a variable (change the "Field_xxx" id by yours)
$featuredArticle = $fields[$fieldId];
// Check if the true/false field is true
if ( $featuredArticle ) {
/* If it's set to true do a WP_query so you can get the other post that have $featuredArticle set to true here you may want to change the 'cat' to whatever your is (or to all) in the meta query you should use your true/false field name as a key */
$args = array(
'cat' => $catId,
'meta_query' => array(
array(
'key' => 'featured_article',
'value' => '1',
'compare' => '=='
)
)
);
$post_query = new WP_Query( $args );
/* Here we check if the query returns post (if it does it will return featured posts) */
if ( $post_query->have_posts() ) {
while ( $post_query->have_posts() ) {
$post_query->the_post();
/* For each posts we update the true/false field and set it to false */
update_field( $fieldId, false, get_the_ID() );
}
}
}
}
}
} // end my_acf_save_post
great, you code is clearer than mine as it uses “$fieldId” and my field_id are hard coded, i will use that on the future 🙂
Hi there,
I can’t make this work.
I have a true/false field called “best of week” in my posts. How exactly do you find the field id?
Here is my code:
add_action('acf/save_post', 'my_acf_save_post', 1);
function my_acf_save_post( $post_id ){
$fields = false;
/* Here I added a if statement so the code runs only for a defined category */
if(get_post( $post_id )){
// Check if Fields have been posted
if ( isset( $_POST['fields'] ) ) {
$fieldId = 'acf-field_57872ec102054-1';
$fields = $_POST['fields'];
// Store the true/false field on a variable (change the "Field_xxx" id by yours)
$BestWeek = $fields[$fieldId];
// Check if the true/false field is true
if ( $BestWeek ) {
/* If it's set to true do a WP_query so you can get the other post that have $BestWeek set to true here you may want to change the 'cat' to whatever your is (or to all) in the meta query you should use your true/false field name as a key */
$args = array(
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'best_of_week',
'value' => '1',
'compare' => '=='
)
)
);
$post_query = new WP_Query( $args );
/* Here we check if the query returns post (if it does it will return featured posts) */
if ( $post_query->have_posts() ) {
while ( $post_query->have_posts() ) {
$post_query->the_post();
/* For each posts we update the true/false field and set it to false */
update_field( $fieldId, false, get_the_ID() );
}
}
}
}
}
}
I found the id from ACF field group, in WP admin choose “screen options” from top and check the “Field Keys” checkbox, that will show Key/id values on the custom fields you have made.
The topic ‘True/false – When new true, update the rest to false’ is closed to new replies.
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.