Reputation:
I have created a new area on a site using Advanced Custom Fields. The area displays on the new posts and modified posts but not on the old posts. What can be done to show it on all posts with minimum effort?
In a 2013 post I saw there was no solution for it back then.
<div id="advert">
<a href="<?php echo get_field( 'add_ad_link' ); ?>">
<img src="<?php echo get_field( 'ad_image' ); ?>">
</a>
</div>
Upvotes: 5
Views: 4530
Reputation: 3401
You could use the idea from Benoti's answer and paste this snippet in your functions.php
. The approach for your case would be:
add_action('admin_init', 'set_default_acf_values');
function set_default_acf_values() {
$args = [
'post_type' => 'post',
'posts_per_page' => -1,
];
$posts = get_posts($args);
foreach($posts as $post) {
if (empty(get_field('add_ad_link', $post->ID))) {
update_field('add_ad_link', DEFAULT_AD_LINK, $post->ID);
}
if (empty(get_field('ad_image', $post->ID))) {
update_field('ad_image', DEFAULT_AD_IMAGE, $post->ID);
}
}
}
Your add_ad_image
field should be set as Image URL to keep this simple.
Note that this code will be executed every time you enter your admin area, so after the first time it would be a good idea to remove it (since all posts will now have a value for those fields).
Upvotes: 5
Reputation: 71
For anyone who comes across this, the best solution that I found was this tool: https://github.com/mcguffin/acf-quick-edit-fields
This saved me an hour or two of manually updating 181 posts. After installing that plugin, I edited the ACF field and checked off the option “Allow editing this field in Bulk edit mode” then I was able to bulk edit the posts and update the field.
Upvotes: 0
Reputation: 36
Maybe you could use the default value of your acf for posts that don't have a value for the field yet.
You can get the default value of an ACF if you know the field key like this:
$field = get_field_object('Your_acf_field_key'));
$default_value = $field['default_value'];
To get the acf key, you have to query the database. I got my query from here: How to get Advanced Custom Fields field key from WordPress database?
So if you only have the fieldname, your code to get the default value would be:
function get_acf_key($field_name) {
global $wpdb;
return $wpdb->get_var("
SELECT `meta_key`
FROM $wpdb->postmeta
WHERE `meta_key` LIKE 'field_%' AND `meta_value` LIKE '%$field_name%';
");
}
$field = get_field_object(get_acf_key('Your_acf_name'));
$default_value = $field['default_value'];
Be aware that the acf key can change in a different environment.
Upvotes: 0
Reputation: 2210
I don't use ACF, but you can do it a loop through all posts and apply the custom field if not exists, with the WordPress functions get_post_meta
and update_post_meta
.
A very simple example, to paste in functions.php :
add_action('admin_init', 'apply_my_custom_field');
function apply_my_custom_field(){
if(!get_option('se_40493675')){
$args = array(
'post_type'=> 'post',
'posts_per_page' => -1,
'post_status'=> 'publish'
);
$posts = get_posts($args);
foreach($posts as $post){
if(!get_post_meta($post->ID, 'your_custom_field_name')){
update_post_meta($post->ID, 'your_custom_field_name', 'your_custom_field_value');
}
}
update_option('se_40493675', 1);
}
}
Change $args as you like ie: 'post_status'=> array('publish', 'pending', 'draft')
.
Upvotes: 3
Reputation: 79
You can do it by bulk edit and then save/update.
Go to your site admin. Then select "All post" from "post" in admin menu. Then checked the "Title" checkbox below "Bulk Actions" box. Now you have selected all post within that page (1-20). Then select "edit" from "Bulk Actions" box. Then click on "Apply". Then click on "Update"(It is in right bottom corner). Repeat for all old posts. That's all. Then The area will display on the old posts.
Please let me know.
Upvotes: -1