Friday, April 3, 2015

Create post object

In the WP_Post class is used to contain a lot of post objects stored by the database and is returned by functions like: get_post.
this is my example create post or page object:

<?php
// Create post object
$my_post = array(
  'post_title'    => 'My post',
  'post_content'  => 'This is my post.',
  'post_status'   => 'publish',
  'post_author'   => 1,
  'post_category' => array(8,39)
);

// Insert the post into the database
wp_insert_post( $my_post );


$post_id = wp_insert_post( $post, $wp_error );
//now you can use $post_id within add_post_meta or update_post_meta

$defaults = array(
  'post_status'           => 'draft',
  'post_type'             => 'post',
  'post_author'           => $user_ID,
  'ping_status'           => get_option('default_ping_status'),
  'post_parent'           => 0,
  'menu_order'            => 0,
  'to_ping'               =>  '',
  'pinged'                => '',
  'post_password'         => '',
  'guid'                  => '',
  'post_content_filtered' => '',
  'post_excerpt'          => '',
  'import_id'             => 0,
  'post_category' => array(5,7)
);

?>
<?php
// Add or Update the meta field in the database.
if ( ! update_post_meta (7, 'fruit', 'banana') ) {
add_post_meta(7, 'fruit', 'banana', true );
};
?>
<?php add_post_meta( 68, 'my_key', '47' ); ?>
<?php add_post_meta( 68, 'my_key', '682' ); ?>
<?php add_post_meta( 68, 'my_key', 'The quick, brown fox jumped over the lazy dog.' ); ?>

<?php
echo'<br>Insert post:'.get_option('default_ping_status');
$defaults = array(
  'post_title'      => 'Post Title',
  'post_content'  => 'This is post content',
  'post_status'           => 'publish',
  'post_type'             => 'listing',
  'post_author'           => 1,
  'ping_status'           => get_option('default_ping_status'),
  'post_parent'           => 0,
  'menu_order'            => 0,
  'to_ping'               =>  '',
  'pinged'                => '',
  'post_password'         => '',
  'guid'                  => '',
  'post_content_filtered' => '',
  'post_excerpt'          => '',
  'import_id'             => 0,
  'post_category' => array(5,7)
);
//$post_id = wp_insert_post( $defaults);
if($post_id){
echo'<br>inserted';
if ( ! update_post_meta ($post_id, 'fruit', 'banana') ) {
add_post_meta($post_id, 'fruit', 'banana', true );
echo'<br>mata inserted';
}
}
echo'<br>';
$catsql = "select * from $wpdb->terms c,$wpdb->term_taxonomy tt  where tt.term_id=c.term_id and tt.taxonomy='listingcategory' and c.name != 'Uncategorized' and c.name != 'Blog'  $substr order by c.name";
echo $catsql;
$term_taxonomy_ids = wp_set_object_terms(386, 5, 'listingcategory' );
 ** Do you know about PHP code Free and open source software (FOSS)?

No comments:

Post a Comment