Thanks for that James. Massively appreciated. A WordPress issue after all!
I have used the wp_set_object_terms() function instead and now it’s working.
Great stuff. Many thanks once again for your guidance.
Including this hook now redirects the front-end visitor to the wordpress login form (?)
As far as I can see from the documentation, it’s the acf/pre_save_post hook I need as this was made with front-end submissions in mind.
Just can’t get it to save the taxonomies!
It worked on a previous version of WordPress (4.5.4), but now it doesn’t since I upgraded to 4.7.
Any more suggestions?
Life saver. Worked like a charm. Big thanks to you sir.
Yep. You were right!
Still have no idea why it was working locally on a single site install.
Anyway, that sorted it. Thank you.
I’m applying some additional form customizing so my code isn’t as black and white as the documentation but the fact remains that it’s working locally but not on a multi site install. I’m baffled by this. Here’s my code:
<form id="post" name="post" class="acf-form" action="" method="post">
<?php
// EMAIL ADDRESS
echo "<div class='apply-section' id='section-email-address'>";
echo "<h3>Email Address</h3>";
acf_form(array(
'form' => false,
'fields' => array('mws_email_address'),
));
echo "</div>";
}
// DATE OF BIRTH
echo "<div class='apply-section' id='section-dob'>";
echo "<h3>Date of Birth</h3>";
acf_form(array(
'form' => false,
'fields' => array('mws_date_of_birth'),
));
echo "</div>";
?>
<!-- SUBMIT -->
<div class="acf-form-submit">
<input type="submit" name="submit" class="button button-primary button-large" value="Submit Application" />
<span class="acf-spinner"></span>
</div>
<!-- / SUBMIT -->
</form>
@hube2, A huge thanks. That got it working.
Here’s my full function that may help other people in the future.
function my_pre_get_posts( $query ) {
// do not modify queries in the admin
if( is_admin() ) {
return $query;
}
// allow the url to alter the query
if( isset($_GET['min_height']) ) {
// Load min_height and max_height as veriables
$min_height = $_GET['min_height'];
if ($_GET['max_height'] !='') {
$max_height = $_GET['max_height'];
} else {
$max_height = 9999999;
}
// Query
$meta_query = array(
array(
'key' => 'mws_height',
'value' => array($min_height, $max_height),
'compare' => 'BETWEEN',
'type' => 'NUMERIC'
)
);
$query->set('meta_query', $meta_query);
// Order by height
$query->set('orderby', 'meta_value_num');
$query->set('meta_key', 'mws_height');
$query->set('order', 'ASC');
}
// return
return $query;
}
add_action('pre_get_posts', 'my_pre_get_posts');
Hi @hube2, thanks again for your support with my previous question. I have another one that’s related.
The above code works for both back-end post creation and also front-end post creation (by public users).
However, I noticed that when looking in the media browser the images, although assigned as thumbnails, are not “attached” to any particular posts.
Is there a way the image can be “attached” to the post too when submitted from the front-end?
I have a plugin that deletes all associated post content upon post deletion (all attached media), which is why I’d like to get this working too.
Here’s the code that created the post from the front end:
add_filter('acf/pre_save_post' , 'my_pre_save_post' );
function my_pre_save_post( $post_id ) {
// Create a new application post
$post = array(
'post_status' => 'publish',
'post_type' => 'application',
);
// insert the post
$post_id = wp_insert_post( $post );
// return the new ID
return $post_id;
};
Many thanks.
Jack
I’m wrapping up this project now but still haven’t found a way to make this happen.
I have successfully managed to query custom fields dynamically using GET parameters by following the instructions at the bottom of this page: view-source:http://www.advancedcustomfields.com/resources/query-posts-custom-fields/
However, how can I return posts with a custom field between two values? IE: ?min-height=50&max-height=150
Many thanks 🙂
Thanks John,
In my attempts I was trying to use add_post_meta again. I completely looked over delete_post_meta.
Thanks for putting me straight 🙂
The topic solution works wonderfully. However, I noticed that when looking in the media browser the images, although assigned as thumbnails, are not “attached” to any particular posts.
Is there a way the image can be “attached” to the post too?
I have a plugin that deletes all associated post content upon post deletion (all attached media), which is why I’d like to get this working.
Many thanks 🙂
Oh dear… Something peculiar happened. The code I posted above was working fine for V4 but I just updated to V5 PRO and suddenly every time I hit update or publish, ALL fields are cleared and return to holding no value.
Any advice would be greatly appreciated.
Many thanks.
Hi John, thanks for this, I didn’t know they were treated similar to a relationship. Here is my current functions.php code based on Elliot’s tutorial:
// array of filters (field key => field name)
$GLOBALS['my_query_filters'] = array(
'example_field' => 'example1',
'example_field_two' => 'example2',
//'music-taste' => 'music-taste' (this is what I'm trying to make work)
);
// action
add_action('pre_get_posts', 'my_pre_get_posts', 10, 1);
function my_pre_get_posts( $query ) {
// 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;
}
// get the value for this filter
// eg: http://www.website.com/events?city=melbourne,sydney
$value = explode(',', $_GET[ $name ]);
// append meta query
$meta_query[] = array(
'key' => $name,
'value' => $value,
'compare' => 'IN',
);
}
// update meta query
$query->set('meta_query', $meta_query);
}
I’m unsure of how to incorporate your suggested code, being as my working queries are ‘compare’ => ‘IN’, and my checkboxes need to be ‘compare’ => ‘LIKE’.
Many thanks!
I was trying to accomplish something similar. Here is how I did it.
My PHP is rusty so if you spot any improvements please do let me know.
I wanted to populate my post title to combine two custom fields: first-name and last-name
function custom_field_value_as_title( $value, $post_id, $field ) {
global $_POST;
// vars
$new_title_first_name = get_field('first-name', $post_id);
$new_title_last_name = get_field('last-name', $post_id);
$new_title = "$new_title_last_name, $new_title_first_name";
//$new_slug = sanitize_title( $new_title );
$result =
// update post
// http://codex.wordpress.org/Function_Reference/wp_update_post
$my_post = array(
'ID' => $post_id,
'post_title' => $new_title,
'post_name' => $post_id //originally $new_slug
);
// Update the post into the database
wp_update_post( $my_post );
}
add_filter('acf/update_value', 'custom_field_value_as_title', 10, 3);
The result in the post title is last-name, first-name. For example: “Obama, Barack”
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.