ACF Form Update duplicates post - wordpress

i’m having a small issue.
Using a CPT, where no title is available, so i’m using the following code to make the title out of 2 ACF fields (first name / last name).
What is happening is that instead of updating, it’s making a new post with the new information. When i disable the script, everything works as intended.
Is there something that I could add to this script so that post update with forms work correctly?
function doctors_title_updater( $post_id )
{
if ( get_post_type( $post_id ) == 'doctors' ) {
$my_post = array();
$my_post['ID'] = $post_id;
$my_post['post_title'] = get_field( 'doctors_lname', $post_id ) . substr(get_field( 'doctors_fname', $post_id ), 0, 30) ;
wp_update_post( $my_post );
}
}
// run after ACF saves the $_POST['fields'] data
add_action('acf/save_post', 'doctors_title_updater', 20);
The code works pefectly fine when i'm editing the post from the back-end and hit update. The issue is only when using the submit from the front-end form.

Related

How to save custom meta only for review data to see only in "Preview Changes", not the actual post in the front end

I am trying to add custom meta and see at "Preview Changes". I can see the changes but also changes apply to the actual post at Front end. I want the changes will update to the actual post when it Publish or Update not at "Preview Changes" click. Please help.
I have followed this plugin.
function my_plugin_save_post( $post_id, $post ) {
if ( $parent_id = wp_is_post_revision( $post_id ) ) {
$parent = get_post( $parent_id );
$my_meta = get_post_meta( $parent->ID, 'my_meta', true );
if ( false !== $my_meta )
add_metadata( 'post', $post_id, 'my_meta', $my_meta );
}
} add_action( 'save_post', 'my_plugin_save_post' );
The following code will prevent your meta data from saving on preview but You won't be able to preview published posts with your metadata. Honestly I'm trying to figure out this situation myself :/
<?php // In your save metabox data function, near the top...
if (isset( $_POST['wp-preview'] ) && 'dopreview' == $_POST['wp-preview'] ) {
if(get_post_status($post_id) == 'publish'){
return; // This way we can still preview draft / scheduled posts
}
}
Honestly, I'd use this code and set your post briefly to draft or private while editing / previewing and publish them as normal when you're done.

Wordpress ACF plugin: trouble getting field when saving post

Using ACF; I am making a plugin that hooks into WP on 'draft_to_publish' and on 'pending_to_publish' this part works fine. But when I try to get the ACF generated fields they return as blank, as if the fields generated by ACF haven't been set yet.
Short version of my plugin looks like this:
function scheduelMailChimp( $post ) {
// get post data and preb it for mail
$post_ID = get_the_ID();
$content_post = get_post( $post_ID );
$content = $content_post->post_content;
$postTitle = get_the_title( $post_ID );
//log debuginfo til debug.log
log_me(
array(
'get field date' => get_field($field_name, $post_id, $format_value)
)
);
}
add_action( 'draft_to_publish', 'scheduelMailChimp', 10, 1 );
add_action( 'pending_to_publish', 'scheduelMailChimp', 10, 1 );
The above code outputs empty. If I try to output something that is generated by WP and not ACF everything works like a charm.
all and any bright ideas are more then welcome :)
In case other people are looking for the same answer: After diving into a ton of Google searches, it seems that ACF haven't saved the data at the time draft_to_publish is fired. So I tried using the 'save_post' and it worked like a charm.

How is clicking 'update' on a post different from programatically creating posts?

hoping for some advice.
I'm programmatically inserting a large number of posts into wordpress from a JSON feed. The wp_insert_post function is working brilliantly and the posts are created, along with correctly populated Advanced Custom Fields meta data.
We have a strange issue by which until we manually click "update" on a single post the custom fields aren't available using a JSON API plugin.
I've tried updating all via the bulk editor, as well as calling wp_update_post after the JSON import. It's as if the act of clicking "update" on a single post saves the post in a different fashion.
Can anyone advise why this would be the case? Any advice or pointing in the right direction would be greatly appreciated!
EDIT: the code we're using to update our post meta...
function __update_post_meta( $post_id, $field_name, $value = '' ) {
if ( empty( $value ) OR ! $value )
{
delete_post_meta( $post_id, $field_name );
}
elseif ( ! get_post_meta( $post_id, $field_name ) )
{
add_post_meta( $post_id, $field_name, $value );
}
else
{
update_post_meta( $post_id, $field_name, $value );
}
}
So we resolved the particular issue we were having.
When a post is programmatically created with ACF fields, the posts are not labelled in the same way as standard post meta. Read here for more info on that!
Updating a post manually creates the necessary "aliases". Until that point, if you want to get the info out, you need to reference ACF's initial "fieldXXXXXXXX" post meta key.

Wordpress - Get Attachment url when publish post

I have a problem. I want to get the featured image url when a post was published.
It works when I update a post, but not when it was published for the first time, because the meta data seems not to be stored in the database at this moment. Even when I use 'wp_insert_post' instead of 'save_post' it does not work.
In my functions.php i check for new/updated posts with:
add_action( 'save_post', 'my_function' );
When a post was updated I read the featured image url by using:
$image_url = get_post_meta( get_post_meta( $post_id, "_thumbnail_id", true ), "_wp_attached_file", true );
Can you help me?
Well, if you want to take attachments from the post you are publishing, save_post is a no go.
Try publish_post
At the moment when publish_post is fired, the post and its attachments already exists in the database and can be accessed.
save_post action hook runs after the data is saved to the database ( wordpress codex ), so this should do it, it works on post publish and post update. A couple of useful links is commented within the code.
// http://codex.wordpress.org/Plugin_API/Action_Reference/save_post
add_action( 'save_post', function ( $post_id ) {
if ( wp_is_post_revision( $post_id ) ) return;
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
// save_post action is executed on all post actions like publish, trash, auto-draft, etc.
// http://codex.wordpress.org/Post_Status_Transitions
if ( get_post_status( $post_id ) == 'publish' ) {
// http://codex.wordpress.org/Function_Reference/get_post_thumbnail_id
$thumb_id = get_post_thumbnail_id( $post_id );
// http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src
$thumb_url = wp_get_attachment_image_src( $thumb_id, 'full' );
$thumb_url = $thumb_url ? $thumb_url[0] : false;
}
});

WordPress 4.0 - Display author's content on custom post type

Here my problem.
I have several users that are allowed to publish content.
I have create a custom post type to be used has a personal page.
I create a new custom post for each of the user.
They can publish media, events, posts... on this custom post, using tools provided by the theme Flawless.
The Flawless tools publish by default all the content, so if you want to add a gallery, it will show all gallery by all users. I want to limit the gallery display to only the current author of the custom post. And the same for all type of content a user may want to add to his page.
My understanding was that pre_get_posts was perfect in this kind of situation.
The goal was to publish on their custom post only their content, so I used the action pre_get_posts with this function:
function only_current_author( $query ) {
if ( !is_admin() && $query->query_vars['post_type'] != 'nav_menu_item' ) {
$post_id = get_queried_object_id();
$author_id = get_post_field( 'post_author', $post_id );
$query->query_vars['author'] = "$author_id";
}
}
add_action( 'pre_get_posts', 'only_current_author' );
This was working fine on my localhost with WordPress 3.9
Now I have updated to WordPress 4.0, and I got an error 404.
I don't understand exactly what cause the issue, and what is different between the two WordPress version, but I think this is what caused the error 404 is that I'm not able to get the author id of the current custom post.
So, I wonder, do you know alternative to define the author id inside the pre_get_posts action ?
Or should I use another method to filter what will be display?
I try several alternative, like adding an extra Query or adding the $query->query_vars in the template in my custom template, but none worked.
I try to get the author id using the name of the custom post found in the $query, nothing seems to work.
Thanks for your help.
So, after a good night of sleep, I return to my problems and found a solution.
Thanks to give me your feedback if you think this solution is appropriate.
function only_current_author( $query ) {
if ( !is_admin() && $query->query_vars['post_type'] != 'nav_menu_item' ) {
global $wp_query;
$post_id = $wp_query->post->ID;
$post_type = $wp_query->post->post_type;
if( $post_id != 0 && $post_type == 'cpt_mdj' ) {
$post_author_id = get_post_field( 'post_author', $post_id );
$query->query_vars['author'] = $post_author_id;
}
}
}
add_action( 'pre_get_posts', 'only_current_author' );
So, the issue was that $post_id = get_queried_object_id(); returned null, from that it was impossible to pre filter the query with the author id.
Using $wp_query, I was able to get the post id, and then the author id.
Side question, what is the difference between $query and $wp_query, they are identical when I var_dump, but give different result.
Thanks!

Resources