Yeah thx, but not quite what i need. I need a way to generate acf admin files, and i only have my php code to work with.
I was wondering, is there any way, for example, to generate a json file, using the php code i have, and then import them again, so they show up in the backend.
Hi, i’m looking for the exact same thing.
I created a fields using ACF in the wp-admin area, then I exported them via php.
Later ( by mistake ) i overwritten my database, and now I can’t edit the fields in the wp-admin, only via php.
Is there any way i can register them again, using the php code i have.
I need this only once, later i will edit them in the wp-admin area.
John, thank you for your help, i figured out what the problem was, after I used your meta_query code, it didn’t work, but then I started to do a bit more research, and it turns out the problem was in the $value var.
I changed
$value = explode(',', $_GET[ $name ]);
to $value = $_GET[ $name ]
and it worked flawlessly.
To help anyone else, my whole code was:
function lang_pre_get_posts( $query ) {
// array of filters (field key => field name)
$GLOBALS['my_query_filters'] = array(
'field_1' => 'event_country'
);
// bail early if is in admin
if( is_admin() ) {
return;
}
// get meta query
$meta_query = $query->get('meta_query');
// loop over filters
foreach( $GLOBALS['my_query_filters'] as $key => $name ) {
// continue if not found in url
if( empty($_GET[ $name ]) ) {
continue;
}
if( ! $query->is_post_type_archive() ) return;
// get the value for this filter
$value = $_GET[ $name ];
// append meta query
$meta_query[] = array(
'relation' => 'OR',
array(
'key' => 'event_country',
'value' => '"'.$value.'"',
'compare' => 'IN'
),
array(
'key' => 'event_country',
'value' => '"'.$value.'"',
'compare' => 'LIKE'
),
);
}
// update meta query
$query->set('meta_query', $meta_query);
}
add_action('pre_get_posts', 'lang_pre_get_posts', 10, 1);
Yes, the field was changed multiple times, from single select field, to multi select field, and to plain text field.
Every time I changed the field, I updated the values again, so it would overwrite the old ones.
No, i tried that already.
The values are stored by a multi-select field, so it gives an array.
I tried with:
$meta_query[] = array(
'key' => $name,
'value' => '"'.$value.'"',
'compare' => 'LIKE'
);
also tried to change the ‘key’ manually to only one value (“event_country”), and tried with the solution you gave me above (3. Multiple custom field values), by modifying code like this :
‘meta_query’ => array(
‘relation’ => ‘OR’,
array(
‘key’ => $name,
‘value’ => ‘”‘.$value.'”‘,
‘compare’ => ‘LIKE’
),
array(
‘key’ => $name,
‘value’ => ‘”‘.$value.'”‘,
‘compare’ => ‘LIKE’
)
)
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.