wp_insert_post not working

by Brock_s34   Last Updated October 17, 2019 09:08 AM

I'm trying to get the movie info from the omdb web service and it worked fine but it doesn't work when I want to use wp_insert_post to create new posts. This script acts as the user enters the movie ID and receives the information as an AJAX and a link is displayed to the user to edit the post but the post id value is 0.

function omdb_Flex_movie(){
add_menu_page(
    'Fetch info',
    'fetch info',
    'edit_posts',
    'test-script',
    'menu_callback',
    'dashicons-format-video',
    1);
}
add_action('admin_menu','omdb_Flex_movie');

function fetch_info(){

    if (current_user_can('edit_posts')) {
        if (isset($_POST['id_movie'])) {
            $id_movie = $_POST["id_movie"];
            if (($id_movie != NULL)) {
                $get_json_movie = file_get_contents("http://www.omdbapi.com/?apikey=ba9e1d33&i=$id_movie");
                $result = json_decode($get_json_movie, TRUE);
                $imdb = $result['imdbID'];
                $genrefarsi = $result['Genre'];
                $my_post = array(
                    'post_title' => 'test title',
                    'post_status' => 'pending',
                    'post_type' => 'post'
                );
                global $wpdb;
                $consulta = "SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = 'id_imdb' AND meta_value = '{$imdb}' ";
                $verificar = $wpdb->get_results($consulta, OBJECT);
                if ($verificar) {
                    echo '<strong>This Post Already Exist</strong> ';
                } else {
                    $post_id = wp_insert_post($my_post);
                    echo '<a target="_blank" href="' . esc_url(home_url()) . '/wp-admin/post.php?post=' . $post_id . '&action=edit"><span class="draft_ready">Click This Link</span></a><span class="import_completed">Success</span>';
                }
                wp_set_object_terms($post_id, $genrefarsi, 'category');
            }
        }
    }

die();
}
add_action('wp_ajax_fetch_info', 'fetch_info');

function menu_callback(){
   ?>
        <!--Tabs Container-->
     <div class="tabs-container">
      <ul class="tabs">
        <li data-tab="film" class="active">movie</li>
        <li data-tab="serial" class="">series</li>
      </ul>
    </div>
   <!--Body Container-->
   <div class="omdb-container active" id="film">
      <div class="form">
       <div class="inputs">
        <input type="text" name="id_movie" placeholder="ID imdb">
        <?php wp_nonce_field('send-movies', 'send-movies-nonce'); ?>
        </div>
        <div class="bottom">
        <button type="button" id="sendAjax">Fetch info</button>
        <p>
        <small>exp :</small>
        http://www.imdb.com/title/<strong>tt2386490</strong>
        </p>
        </div>
        </div>
        <div class="loading"><img src="<?php echo DT_DIR_URI ;?>/inc/omdb/img/loading.svg"></div>
        <div class="result"></div>
        </div>
      }


Answers 1


I guess the reason is you have not added post title and added post id

$my_post = array(
                'post_id' => 1,
                'post_status' => 'pending',
                'post_type' => 'post'
            );

Ideally if should be

$my_post = array(
  'post_title'    => wp_strip_all_tags( $_POST['post_title'] ),
  'post_content'  => $_POST['post_content'],
  'post_status'   => 'publish',
  'post_author'   => 1,
  'post_category' => array( 8,39 ),
'post_type' => 'post'
);
Pratikb.Simform
Pratikb.Simform
October 17, 2019 08:29 AM

Related Questions


Insert post in two database

Updated November 14, 2016 07:32 AM




WordPress hook which triggers on post import

Updated May 16, 2019 18:08 PM