I am using WordPress's Advanced Custom Fields 5 plugin to create a front-end form. I would like to save the post title as one of the fields on my form. For example, one of my form fields is 'name' so I would like the post title to be 'John Smith'.
Looking at the ACF documentation it gives example code (copied below) where pre_save_post can be hooked into achieve this. Hoewver, I have included this function and the title still fails to save.
Any ideas what I am doing wrong?
Here is the code:
function my_pre_save_post( $post_id )
{
// check if this is to be a new post
if( $post_id != 'new' )
{
return $post_id
}
// Create a new post
$post = array(
'post_status' => 'draft' ,
'post_title' => $_POST['fields']['field_123'] ,
'post_type' => 'post' ,
);
// insert the post
$post_id = wp_insert_post( $post );
// return the new ID
return $post_id;
}
add_filter('acf/pre_save_post' , 'my_pre_save_post', 10, 1 );
I am using exactly the same method and it works fine.
Just a note that field_123 in your example is not the user defined field name in the wp back end but its actually the field name assigned in the meta_key value in the wp_postmeta table of you db.
Related
So bascially we created a custom post type with a plugin called Toolsets and added a custom field to the rest-api called parentpagename.
The code looks like this:
add_action( 'rest_api_init', 'create_api_posts_meta_field' );
function create_api_posts_meta_field() {
register_rest_field( 'comp-tourseite', 'parentpagename', array( //replace test-cpt-1 with your child post type slug
'get_callback' => 'get_parent_page_for_api',
'schema' => null,
)
);
}
function get_parent_page_for_api( $object ) {
//get the id of the post object array
$post_id = $object['id'];
$toolset_parent_id = toolset_get_related_post( $post_id, 'comp-tour-comp-tourseite' ); // replace page-test-cpt-1 with one-to-many relationship slug
//return the post meta
return esc_html(get_the_title($toolset_parent_id));
}
So far so good. Everything works fine and the custom field gets populated accordingly when using the rest api.
However we can't filter the results using our custom field. For example using ?parentname=test at the end of our URL does not work. It shows all results.
slug however works perfectly fine.
This issue seems to have something to do with our field not beeing added in the accepted variables defined by wordpress.
How can we make our field filterable?
I want to automatically set my post title according to custom meta fields for example my custom meta fields have a Name of user I want the title of the post to be the Name of User hope you can help me with this
You can use WordPress' "save_post" hook to change the post title according to post meta whenever a post is saved. Lets say you fave a post meta field called "name_of_user" and you want to change the post title accordingly
add_action('save_post', 'wpse_update_post_title');
function wpse_update_post_title($post_id) {
// If this is just a revision, don't update the title yet.
if ( wp_is_post_revision( $post_id ) ) {
return;
}
// unhook this function so it doesn't loop infinitely
remove_action('save_post', 'wpse_update_post_title');
$user_name = get_post_meta( $post_id, 'name_of_user', true );
// Check if the meta for given key exists and then update the title
if($user_name) {
$slug = str_replace(' ', '-', strtolower($user_name))
$post_update = array(
'ID' => $post_id,
'post_title' => $user_name,
'post_name' => $slug // This swill update the url slug of the post too`enter code here`
);
wp_update_post( $post_update );
}
// re-hook this function
add_action('save_post', 'wpse_update_post_title');
}
The above code should go into your theme's functions.php file
I am working on an app that has a post form that matches up adoption mentors with mentees.
This is in Wordpress/PHP/MySQL/ACF (Advanced Custom Field).
After the form has been submitted, I am unable to get the post_id for that post so I can save the ACF field "title" for the screen that lists all the matches.
Do I need to retrieve that form's $post_id when I'm "outside the loop"? And how do I do that?
function match_post_title_auto( $post_id ) {
// get mentor & new mentee user array
$mentee = get_field('match_mentee', $post_id);
$mentor = get_field('match_mentor', $post_id);
$title = ' Mentor: ' . $mentor['display_name'] . ' and Mentee: ' . $mentee['display_name'];
$postdata = array(
'ID' => $post_id,
'post_title' => $title,
'post_type' => 'match'
);
wp_update_post( $postdata );
return $value;
}
var_dump($post_id); //returns NULL
//what do I put here to get that `post_id`? Thanks!
add_action('acf/save_post', 'match_post_title_auto', 10, 1);
The acf/save_post hook only passes the $post_id as single parameter. You have 3 parameters in your callback. https://www.advancedcustomfields.com/resources/acf-save_post/. Also, you need to have a priority higher than 10 to get the updated values from your post.
function match_post_title_auto( $post_id ) {
// do stuff
}
add_filter('acf/save_post', 'match_post_title_auto', 20 );
you are trying to catch the values by filter hook. Actually you need action hook and 1 parameter only. like this:
add_action('acf/save_post', 'match_post_title_auto', 10, 1);
Hope it will work.
for more information, check the standard documentation:
https://www.advancedcustomfields.com/resources/acf-save_post/
We have an in-house WordPress plugin that creates content using a custom-post-type. Essentially, the content created is, for all intents and purposes, identical to WordPress 'Pages'.
The idea is that we add these custom 'pages' - via our plugin - and then deploy/update them for whomever is using the plugin. We've used our plugin to create a series of these pages, but the question is how to port this custom content with the plugin.
Initially we were thinking that since our posts have a custom post-type, we can easily identify them in the DB. In fact, we use that to remove our custom content when uninstalling the plugin.
But how do we do the inverse? Ideally we'd like to update the plugin, and with it, any custom content that we've added or modified.
Originally we were thinking of just using MySQL scripts to add this custom post content, but after a bit of research, this doesn't seem like the right way to do it.
I know WordPress has a wp_insert_post() function, but I'm just not sure of how it all fits together.
Ideally, the answer would be an overview of the process for updating our custom content. For instance, should there be a function in our plugin that, upon installation, looks for a sql file and creates new posts from it?
Thanks!
If I understand this correctly and you are trying to keep content sync'ed across multiple installations via plugin updates you will need to create a unique ID for all of your content. When you do updates programatically you are updating by the post ID but each installation will have different post IDs. You could use a post meta field to give each post a unique ID specific to your plugin. You can then query the posts based on your unique meta field.
I would code the plugin so it checks to see if there is a post with your unique ID. If it is not found then you insert a new post. If it is found then you update the post.
Let's say you have created your first piece of content and it's unique id is going to be jgohil_1. Your plugin might use something like the following to check for that unique id and then update or insert depending on if it exists.
<?php
// our meta key/value pair for our unique id
$meta_key = 'jgohil_unique_id';
$meta_value = 'jgohil_1';
// set our new content
$new_content = 'The new content here';
$new_title = 'The new title here';
// check the database to see if we have created the post already by querying for our unique id
global $wpdb;
$sql = $wpdb->prepare (
"SELECT post_id
FROM $wpdb->postmeta
WHERE meta_key = %s
AND meta_value = %s",
$meta_key,
$meta_value
);
$post_id = $wpdb->get_var( $sql );
// if we got a post id then update else insert
if ( $post_id ) {
// set up the data for updating
$data = array(
'ID' => $post_id,
'post_content' => $new_content,
'post_title' => $new_title,
);
// update the post
$updated = wp_update_post( $data );
if ( is_wp_error( $updated ) ) {
// do some error handling here
}
} else {
$data = array(
'post_title' => $new_title,
'post_content' => $new_content
'post_type' => 'your_custom_post_type'
);
$post_id = wp_insert_post( $data );
// if the insert worked give the post the unique meta id
if ( ! is_wp_error( $post_id ) && $post_id !== 0 ) {
update_post_meta( $post_id, $meta_key, $meta_value );
} else {
// do some error handling here
}
}
If you need to handle multiple pieces of content at once you would want to use a loop and change the meta value each time.
i created a custom table in my wp table called wp_publish fields : title, url, postdateword
i want to insert post data when post is published
for sample
I publish a new post and the post contains url, title, details
insert 3 field on wp_publish automatic.
it is possible ? i am creating plugin.
You can use the save_post, it is an action triggered whenever a post or page is created or updated
add_action( 'save_post', 'save_data' );
function save_data($post_id ){
// check $post_id in table if not exists in table insert,
}
please see this link http://codex.wordpress.org/Plugin_API/Action_Reference/save_post
You can try it works when new post publish
function authorNotification($post_id)
{
$post = get_post($post_id);
$wpdb->show_errors();
global $wpdb;
$wpdb->insert( 'tablename', array( 'name' => "inserted_text"), array('%s'));
$wpdb->print_error();
$wpdb->hide_errors();
}
add_action('publish_post', 'authorNotification');