George Oiko
George Oiko

Reputation: 1433

Wordpress - Check if post exists,if yes update fields

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

              } 
        ?>

Upvotes: 0

Views: 4853

Answers (1)

The Alpha
The Alpha

Reputation: 146239

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.

Upvotes: 3

Related Questions