Home › Forums › Backend Issues (wp-admin) › ACF make a post private if there are no rows in a repeater field.
Hi,
I have posts called seminars and each seminar has a repeater field called sub_seminars with Venue Start_date and End_Date in it.
Some posts have a single row while others have multiple depending if the seminar has only one venue or multiple.
Now I have developed the logic to remove the row from the repeater where start_date is expired.
My question is, if a seminar has only one row of the repeater field and the start_date expires, the row will be deleted leaving the seminar with no rows at all.
I want to set this seminar (with no repeater rows) to private or draft so people can’t see it in the front end.
Any idea how this can be achieved ?
My code to delete the row with expired date
$ap = get_post_meta($post->ID,'sub_seminars_0_start_date',true);
$startdate = date("Ymd", strtotime($ap));
$todaydate = date("Ymd");
if(strtotime($todaydate) > strtotime($startdate) && !empty($ap)){
$del_data = array(
'Ref' => 'sub_seminars_0_ref',
'Start date' => 'sub_seminars_0_start_date',
'End Date' => 'sub_seminars_0_end_date',
'Venue' => 'sub_seminars_0_venue',
'Fees' => 'sub_seminars_0_fees',
'CPE Credits' => 'sub_seminars_0_cpe_credits'
);
delete_row('sub_seminars', 1);
}
Thanks
Hi @damon ,
Thanks for reaching out to us.
You can check the repeater row counter after your method as
$row = count( get_field('repeater_field') );
You can then place an if to check if the number of rows is 0 then update post to a draft.
Hopefully this helps 🙂
Could you give a little example how to set the post to private after counting the rows ?
I mean I don’t know the function to set the post to private ..
Hi @damon ,
Thanks for the follow up.
You make use of the wordpress update_post to set the post status to draft.
Here is a related read .
https://codex.wordpress.org/Function_Reference/wp_update_post
Hope this helps.
Hi @damon ,
Thanks for the follow up.
You make use of the wordpress update_post to set the post status to draft.
Here is a related read .
https://codex.wordpress.org/Function_Reference/wp_update_post
Hope this helps.
$ap = get_post_meta($post->ID,'sub_seminars_0_start_date',true);
$startdate = date("Ymd", strtotime($ap));
$todaydate = date("Ymd");
if(strtotime($todaydate) > strtotime($startdate) && !empty($ap)){
$del_data = array(
'Ref' => 'sub_seminars_0_ref',
'Start date' => 'sub_seminars_0_start_date',
'End Date' => 'sub_seminars_0_end_date',
'Venue' => 'sub_seminars_0_venue',
'Fees' => 'sub_seminars_0_fees',
'CPE Credits' => 'sub_seminars_0_cpe_credits'
);
delete_row('sub_seminars', 1);
}
$row = count( get_field('sub_seminars') );
if ($row == 0) {
$postid = $post->ID; //Supply post-Id here $post->ID.
wp_update_post(array(
'ID' => $postid,
'post_status' => 'draft'
));
}
There seems to be someproblem with my code. Please take a look
Hi @damon ,
Thanks for the follow up.
Your code seems just fine.
It is possible that the $row is not returning a 0 value.
Kindly try a var_dump of the variable to ensure that the correct values are used.
Let me know how it goes.
Hi James,
Could you please give me some more info, perhaps an example please.
Thanks
Hi @damon ,
Here is some sample code that may help you debug your code.
$row = count( get_field('sub_seminars') );
var_dump($row); //this should output the number of rows on returned for you to ensure that the correct value is returned.
var_dump($post->ID) //also confirm that the correct value is in use
if ($row == 0) {
$postid = $post->ID; //Supply post-Id here $post->ID.
wp_update_post(array(
'ID' => $postid,
'post_status' => 'draft'
));
Let me know how it goes.
Hi @james
Thanks for the help, but the problem still remains the same.
//$row = count( get_field('sub_seminars') );
//var_dump($row); //this should output the number of rows on returned for you to ensure that the correct value is returned.
//var_dump($post->ID) //also confirm that the correct value is in use
//if ($row == 0) {
$postid = $post->ID; //Supply post-Id here $post->ID.
wp_update_post(array(
'ID' => 123,
'post_status' => 'draft'
));
//}
This does set the post with id 123 to draft, so that means I have some problem with the count function.
Hi @damon ,
That is quite odd.
Kindly check to confirm that your get_field returns the correct array of sub_fields.
Hopefully this helps.
Hi @james,
I have multiple repeater rows but still when I echo the count ()
function, it shows me that only 1 row is there.
<?php $rows = count(get_field('sub_seminars'));
echo count($rows); ?>
I don’t know what is wrong, and this is giving me nightmares.
Even the repeater that has no rows, it shows count as 1
The topic ‘ACF make a post private if there are no rows in a repeater field.’ 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.