I write following code to save post in wordoress wp_post database table.

but don't save in database

  function writeMsg(){
     global $wpdb;
     $result = $wpdb->get_results ( "SELECT * FROM wp_farsc" );
     $data = null;
   $data.= '<table border="2px" cellspacing="5px" width="100%">'.PHP_EOL;
   $data.= '<tr bgcolor="yellow"><th>سکه</th><th>قیمت انلاین</th><th>تغییر قیمت</th><th>کمترین</th><th>بیشترین</th><th>زمان بروز رسانی</th></tr>'.PHP_EOL;

   foreach ( $result as $row ) {

      $data .='<tr>';
      $data .= '<td>'.$row-> titile.'</td>';
      $data .='<td>'.$row -> liveprice.'</td>';
      $data .='<td>'.$row -> changing.'</td>';
      $data .= '<td>'.$row-> lowest.'</td>';
      $data .= '<td>'.$row-> topest.'</td>';
      $data .= '<td>'.$row-> time.'</td>';
      $data .='</tr>'.PHP_EOL;
 return $data;
 $oiobz1 = writeMsg(1, 'center', '', 1);

function importposts() {
    $postdate = date('2014-06-19 00:30:00');
    $postdate_gmt = date('2014-06-19 04:30:00');
    $titles=' 111111111111111111111111111';
    $posts = array(
        'post_content'   => $oiobz1,
        'post_name'      => $titles,
        'post_title'     => $titles,
        'post_type'      => 'post',
        'post_author'    => '1',
        'ping_status'    => 'publish',
        'to_ping'        => 'http://rpc.pingomatic.com/',
        'post_date_gmt'  => $postdate_gmt,
        'post_date'  => $postdate,

    $post_id = wp_insert_post($posts);
    add_post_meta($post_id, 'META-KEY-1', 'META_VALUE-1', true);

  • where do you call importposts?
    – Milo
    Feb 7, 2015 at 20:51
  • Nowhere......... @Milo
    – Mortzea
    Feb 7, 2015 at 20:54

1 Answer 1


As Milo noted, you need to call the importposts functions to have it do anything.

Additionally, if your post is not inserted, post_id should hold a wp_error object, so you could see what is wrong by doing the following right before add_post_meta

if ( is_wp_error( $post_id ) ) {
   echo "<p>Bad Post Attempt:</p><pre>" . print_r($post_id, true) . "</pre>";

That should point to any problem with your query provided you call the function and there is a query.

Calling the function... (just in case)

After you create your function, it needs to be called:


Personally, I'd give it a more distinct name ... and you probably need to wrap call it via an action that occurs sometime after wordpress has loaded

function do_my_import_posts_now() {
add_action('wp', 'do_my_import_posts_now');

That will ensure that wordpress is set up before your function is called.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.