Wordpress - Check if post exists,if yes update fields - wordpress

I use a frontend form to post, and what i want is to check if a post exists by title.If yes and metafield value is bigger than the old one,just replace the value.
Anyone that has implemented something like that in the past?
<?php
session_start();
$user_email = $_SESSION['user_email'];
$user_name = $_SESSION['user_name'];
$user_img_url = 'https://graph.facebook.com/'.$user_name.'/picture?width=200&height=200';
global $wpdb;
global $post;
$title = $user_name; // get the inputted title
$content = $_POST['content']; // get the inputted content
$categorie = $_POST['cat']; // get the category selected by user
$zombies = $_POST['zombies'];
$kliks = $_POST['klik'];
$timess = $_POST['times'];
$name = $_POST['namn'];
if( 'POST' == $_SERVER['REQUEST_METHOD'] ) { // if form has been submitted
$my_post = array(
'post_title' => $title,
'post_content' => $content,
'post_status' => 'publish',
'post_author' => 2,
'post_category' => array(2),
);
$my_post = wp_insert_post($my_post);
add_post_meta($my_post, 'Zombies', $zombies);
add_post_meta($my_post, 'klik', $kliks);
add_post_meta($my_post, 'times', $timess);
add_post_meta($my_post, 'namn', $name);
add_post_meta($my_post, 'profile_photo', $user_img_url);
wp_redirect( home_url() );
# if $verifica is not empty, then we don't insert the post and we display a message
}
?>

Your question is not quite clear to me but if you want to query a post by it's title then you can use get_page_by_title() function as given below
$post = get_page_by_title( $_POST['post_title'], OBJECT, 'post' );
To get a custom meta field you can use get_post_meta() function as given below
$meta_value = get_post_meta($post->ID, 'field_name', true);
Then compare and update the meta value you can use, for example,
if( $_POST['custom_meta_field'] > $meta_value )
{
// Update the meta value
update_post_meta( $post->id, 'field_name', $meta_value );
}
The update_post_meta() function is being used to update the custom meta field.
Update: (Based on comment)
You can use following to get the post that is available by Facebook ID
$post = get_page_by_title( $_POST['facebook_id'], OBJECT, 'post' );
Also if the meta field is time string (12:10 am) then you have to convert it to timestamp/numeric value before you compare it, like,
$meta_value = strtotime(get_post_meta($post->ID, 'field_name', true));
So, it'll become something like 1363493400 and you can compare like
if( $_POST['custom_meta_field'] > $meta_value ){ ... }
In this case your custom_meta_field should be also a timestamp/numeric value or you have to convert it using strtotime() function just like $meta_value has been converted.

Related

How to update post author after update post

I'd like to update post_author after update post by getting current user ID, but when I use this function wordpress created new post with correct author by copied current post
function change_author () {
if ( ! wp_is_post_revision( $post_id ) ){
$post= array(
'ID' => $post_id,
'post_author' => get_current_user_id(),
);
wp_update_post( $post );
}
}
add_action('save_post', 'change_author');
I'd use the wp_insert_post_data hook instead of save_post. Rather than updating the post after it's saved, it's probably better to change the data before it gets inserted into the database as part of the save post action. Here's what I'd do:
function change_author ( $data ) {
if ( ! wp_is_post_revision( $data['ID'] ) ){
$data['post_author'] = get_current_user_id();
}
return $data;
}
add_filter( 'wp_insert_post_data', 'change_author', 10, 1 );

Updating GEO my WP fields using frontend Advanced custom fields form - WordPress

I’ve created a frontend form using the advanced custom fields (ACF) plugin. Within this form I’ve added some location fields such as postcode and city.
Below is the function used to update the post
global $post_id;
add_filter('acf/pre_save_post' , 'tsm_do_pre_save_post' );
function tsm_do_pre_save_post( $post_id ) {
// Bail if not logged in or not able to post
if ( ! ( is_user_logged_in() ) ) {
return;
}
// check if this is to be a new post
if( $post_id != 'new_job' ) {
return $post_id;
}
$profile_id = um_profile_id();
$userID = 'user_'.$profile_id;
$user_id = get_current_user_id();
// Create a new post
$post = array(
'ID'=> $post_id,
'post_type' => 'members',
'post_status' => 'publish',
'post_title' => $userID,
'post_author' => $user_id,
'category' => $_POST['acf']['field_594d0ffc2a66d'],
);
// insert the post
$post_id = wp_insert_post( $post );
// Save the fields to the post
do_action( 'acf/save_post' , $post_id );
return $post_id;
exit;
}
Once this form has been submitted, the custom post type is updated with no issues.
How can I add the submitted location data (postcode, city etc) into the GEO MY WP information for the post?
I’ve tried to follow the information in the docs secetion (http://docs.geomywp.com/gmw_pt_update_location/) but not having much luck.
GEO my WP function fron docs below:
function gmw_update_post_type_post_location( $post_id ) {
// Return if it's a post revision
if ( false !== wp_is_post_revision( $post_id ) )
return;
// check autosave //
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
//check if user can edit post
if ( !current_user_can( 'edit_post', $post_id ) )
return;
//get the address from the custom field "address"
$address = get_post_meta( $post_id, 'address', true );
//varify that address exists. Otherwise abort the function.
if ( empty( $address ) )
return;
//include the update location file file
include_once( GMW_PT_PATH .'/includes/gmw-pt-update-location.php' );
//make sure the file included and the function exists
if ( !function_exists( 'gmw_pt_update_location' ) )
return;
//Create the array that will pass to the function
$args = array(
'post_id' => $post_id, //Post Id of the post
'address' => $address // the address we pull from the custom field above
);
//run the udpate location function
gmw_pt_update_location( $args );
}
//execute the function whenever post type is being updated
add_action( 'save_post_post', 'gmw_update_post_type_post_location' );
I believe I need to somehow combine the two functions so when the ACF form is updated, it also needs to update the GEO my wp meta data also.
I've tried combining the two functions but not having much luck.
If someone can point me in the right direction it will be very much appreciated
Thanks
Updated code.....
I've tried to add the gmw_update_post_type_post_location() function within the ACF function (code below) but still not having much luck...
global $post_id;
add_filter('acf/pre_save_post' , 'tsm_do_pre_save_post' );
function tsm_do_pre_save_post( $post_id ) {
// Bail if not logged in or not able to post
if ( ! ( is_user_logged_in() ) ) {
return;
}
// check if this is to be a new post
if( $post_id != 'new_job' ) {
return $post_id;
}
$profile_id = um_profile_id();
$userID = 'user_'.$profile_id;
$user_id = get_current_user_id();
$profilePostcode = get_post_meta( $post_id, 'location_postcode', true );
// Create a new post
$post = array(
'ID'=> $post_id,
'post_type' => 'members', // Your post type ( post, page, custom post type )
'post_status' => 'publish', // You can use -> publish, draft, private, etc.
'post_title' => $userID, // Post Title ACF field key
'post_author' => $user_id,
'category' => $_POST['acf']['field_594d0ffc2a66d'], // Post Content ACF field key
);
// insert the post
$post_id = wp_insert_post( $post );
// Save the fields to the post
do_action( 'acf/save_post' , $post_id );
gmw_update_post_type_members_location($post_id);
return $post_id;
exit;
}
function gmw_update_post_type_members_location( $post_id ) {
$profilePostcode = get_post_meta( $post_id, 'profile_location_postcode', true );
if ( empty( $profilePostcode ) )
return;
$address = array(
//'street' => $_POST['location_address'],
//'city' => $_POST['location_town'],
//'state' => $_POST['location_state'],
'zipcode' => $profilePostcode,
//'country' => $_POST['location_country']
);
include_once( GMW_PT_PATH .'/includes/gmw-pt-update-location.php' );
if ( !function_exists( 'gmw_pt_update_location' ) )
return;
$profile_id = um_profile_id();
$userID = 'user_'.$profile_id;
$user_id = get_current_user_id();
$args = array(
'post_id' => $post_id, //Post Id of the post
'post_title' => $userID, // Post Title ACF field key
'post_author' => $user_id,
'post_type' => 'members',
'address' => $address
);
//run the udpate location function
gmw_pt_update_location( $args );
}

Wordpress update button works but not publish button

I asked the programmer to have the title of every post automatically be the date selected and the place selected (to allow users to save some time). When I click 'publish' the title of the post is the date twice (ie; 03-26-15 03-26-15). Then the 'publish' button turns into an 'update' button. And then when I click 'update' the title will be correct (ie; 03-26-15 London). I'm trying to figure out how to get it this way after clicking 'publish' the first time. The programmer disappeared and I can't figure it out. Any help would be great.
function post_updated( $post_id ) {
global $post_type;
if ($post_type == 'place') {
if ( !wp_is_post_revision( $post_id ) && !(defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE) ) {
$postTitle.=get_the_date( 'm/d/Y', $post_id );
$postTitle.=" ".get_the_title(get_post_meta($post_id, 'place_locale', true) ) ;
$post['ID'] = $post_id;
$post['post_title'] = $postTitle;
remove_action( 'save_post', 'post_updated' );
wp_update_post($post);
add_action( 'save_post', 'post_updated' );
}
}
}
add_action( 'save_post', 'post_updated' );
The .= operator appends, therefor your titles are being duplicated.
Simply change it to = and you should be fine.
Are you sure this is the only function? It seems to only have "update post" code.
Post creation is handled with wp_insert_post something like:
$my_post = array(
'post_title' => $title,
'post_content' => "",
'post_status' => "pending",
'post_author' => wp_get_current_user()->ID
);
$post_id = wp_insert_post( $my_post );
Old question, but this may help who got into this question:
There are some problems with the code:
global $post_type is not defined.
As mentioned by #dojs, you should use = operator instead of .= to define the variable first.
get_the_title() function only accepts post_id or post object as the param.
Since you're dealing with a custom post type of place, it's better to use a post type specific hook instead of the general and crowded save_post hook.
After these modifications, the final code could be something like this:
function tst_post_updated( $post_id )
{
if ( !wp_is_post_revision( $post_id ) && !(defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE) ) {
$post_title = get_the_date( 'm/d/Y', $post_id );
$post_title .= " " . get_post_meta($post_id, 'place_locale', true);
$post_title = sanitize_title($post_title);
$post['ID'] = $post_id;
$post['post_title'] = $post_title;
remove_action( 'save_post_place', 'tst_post_updated' );
wp_update_post($post);
add_action( 'save_post_place', 'tst_post_updated' );
}
}
add_action( 'save_post_place', 'tst_post_updated' );

Get Permalink by Slug function not working

I hadn't realised that WordPress had added a native function for it to the Codex, but for some odd reason the page is blank. Does that mean the functionality is still coming or the page was added by mistake?
http://codex.wordpress.org/Function_Reference/get_permalink_by_slug
It's blank because it doesn't exist. If you change that function name to any random name you like you'll still see a blank page (unless of course the name you change it to is a real function).
function get_permalink_by_slug( $slug, $post_type = '' ) {
// Initialize the permalink value
$permalink = null;
// Build the arguments for WP_Query
$args = array(
'name' => $slug,
'max_num_posts' => 1
);
// If the optional argument is set, add it to the arguments array
if( '' != $post_type ) {
$args = array_merge( $args, array( 'post_type' => $post_type ) );
} // end if
// Run the query (and reset it)
$query = new WP_Query( $args );
if( $query->have_posts() ) {
$query->the_post();
$permalink = get_permalink( get_the_ID() );
} // end if
wp_reset_postdata();
return $permalink;
}

Wordpress update published date

when I update post published date from meta box field date it's not working. I am using save_post hook, but it is not working properly.
Which function is used to update post date.
My code:
// Update publisged date from date of launch for concept
function update_concept_post_date( $post_id ){
if ( ! wp_is_post_revision( $post_id ) ){
$post_data = get_post($post_id);
$date = get_post_meta($post_id, '_cmb_concept_signup_date', true);
$updated_date = explode('-', $date);
$new_date = date("Y-m-d h:i:s", mktime(0,0,0,$updated_date[1],$updated_date[2],$updated_date[0]));
if($post_data->post_type == 'concepts'){
$my_post = array(
'ID' => $post_id,
'post_date' => $new_date
);
// update the post, which calls save_post again
wp_update_post( $my_post );
}
}
}
add_action('save_post', 'update_concept_post_date', 10);
You are parsing date wrong, it should be;
$temp = explode(' ', $date);
$updated_date = explode('-', $temp[0]);
First parse it by space character, and first part becomes Y-m-d and then parse this part by -.

Resources