Hello,
I have two content blocks (A and B) on my front page which I want to fill with post teasers. Each block has 6 positions, A1-A6 and B1-B6.
I created custom select field containing options with the values A1-B6 and an option with „X0 – not on front page“ which is the standard value.
Then I created two WP_Queries, one for each Block:
$args_a = array(
'numberposts' => 6,
'post_type' => 'post',
'meta_key' => 'position',
'meta_value' => '^A',
'meta_compare' => 'REGEXP',
'order_by' => 'meta_key',
'order' => 'ASC'
);
$args_b = array(
'numberposts' => 6,
'post_type' => 'post',
'meta_key' => 'position',
'meta_value' => '^B',
'meta_compare' => 'REGEXP',
'order_by' => 'meta_key',
'order' => 'ASC'
);
And put them in two different loops, like it’s shown in the documentation. So far, everything works fine, when I change the position from A to B, the teaser appears in the other box.
Then I decided to try if the position changes could be automated somehow. I created another custom select field with the same options and named it „position_new“ and one datetime field named it „switchtime“. The switchtime is in the future and when it is reached, the field position should be updated with the value from position_new and after that the values of the fields switchtime and position_new have to be deleted.
First I created another WP_Query which gets all posts that are scheduled:
// Get Scheduled Posts
$scheduled = array(
'numberposts' => -1,
'post_type' => 'post',
'order' => 'ASC',
'order_by' => 'meta_value',
'meta_key' => 'switchtime',
'meta_type' => 'DATETIME'
);
Here is the first problem, there are all posts inside the loop, including the posts, where the field switchtime is empty. Deleting meta_key and meta_type from the args does not have an effect.
And then I wrote inside the loop (the first part of the if statement is there to get rid of empty values):
if ( ( get_field("switchtime") != "" ) && ( current_time('Y-m-d H:i:s') > get_field("switchtime") ) ) {
$postid = get_the_ID();
$newpos = get_field("position_new");
update_field('position', $newpos, $postid);
delete_field('switchtime');
delete_field('position_new');
// wp_insert_post( array('ID' => $postid) );
}
At first sight, it seems to work, the field position is updated and the other two fields are deleted on switchtime, but my teaser which I moved from A to B still appears in content block A. I displayed the field value in the frontend and it clearly says B, but the WP_Query from A fetches it anyway. I have to edit the post in the backend simply by saving it without changing anything and after that the teaser appears in B.
Therefore I wondered if that last line in the if statement which should save the post would solve the problem, but no.
Do you know what to do?
Cheers,
Pierre
Sorry, I have picked the wrong category, please change it to frontend or what matches best.
And my third WP_Query initially was like this, but fetched empty values anyway:
$scheduled = array(
'numberposts' => -1,
'post_type' => 'post',
'order' => 'ASC',
'order_by' => 'meta_value',
'meta_key' => 'switchtime',
'meta_value' => '',
'meta_compare' => '!=',
'meta_type' => 'DATETIME'
);
You must be logged in to reply to this topic.
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’re reaching out to our multilingual users to ask for help in translating ACF 6.1. Help make sure the latest features are available in your language here: https://t.co/TkEc2Exd6U
— Advanced Custom Fields (@wp_acf) May 22, 2023
© 2023 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.