Adding a string to wordpress title not adding to backend - wordpress

I have been inserting title into the wordpress post title from front end it was not storing the string as it is.
$title = $_POST['title'];
$new_help = array(
'post_title' => $title,
'post_status'=> 'publish'
);
wp_insert_post();
the string i am inserting is <<hello_world>>
It was storing only <<>>

I think you just forgot to incorporate the arguments into the wp_insert_post function.
if( isset($_POST['title']) && !empty($_POST['title']) )
{
$title = $_POST['title'];
$new_help = array(
'post_title' => $title,
'post_status'=> 'publish'
);
wp_insert_post($new_help);
}

Related

WordPress XMLRPC - Search Post by Slug and Update

Is there anyway to search posts by slug through XMLRPC
https://codex.wordpress.org/XML-RPC_WordPress_API/Posts#wp.getPosts
getPosts() doesn't seem to return using "name"..
$args = array(
'name' => 'my-slug',
'number' => 1
);
$post = $wpClient->getPosts( $args );
Please let me know if there is a workaround for this, I need to search by slug and then update those slugs remotely via XMLRPC. cheers
I ended up using Methods, this may help someone and save time.. paste the following code in functions.php of the domain you are fetching data from
add_filter('xmlrpc_methods', 'clx_xmlrpc_methods');
function clx_xmlrpc_methods($methods) {
$methods['getPostBySlug'] = 'clx_getpost';
return $methods;
}
function clx_getpost($args) {
global $wp_xmlrpc_server;
$slug = $args["slug"];
$pargs = array(
'name' => $slug,
'post_type' => 'post',
'numberposts' => 1
);
$my_posts = get_posts($pargs);
if( $my_posts ) :
return $my_posts; //echo $my_posts[0]->ID;
endif;
}
from your XMLRPC code use the following to get POST array from slug
$args = array(
'slug' => 'your-post-slug'
);
$postArray = $wpClient->callCustomMethod( 'getPostBySlug', $args );

Wordpress Insert post create tne meta key and meta value

I am creating wp_insert_post to create post from json, while creating post i need to create meta values i have tried both add_post_meta and update_post_meta no results Can anybody help
foreach ( $response->data as $single_data ) {
$post_title = $single_data->name;
$video_id = $single_data->uri;
$thumnbnail_url = $single_data->pictures->sizes[4]->link;
if (!post_exists($post_title)) { // Determine if a post exists based on title, content, and date
$post_id = wp_insert_post(array(
'post_type' => 'vimeo_videos',
'post_title' => $post_title,
'post_status' => 'publish',
));
}
$newPostID = wp_insert_post($post_id);
global $post;
add_post_meta( $post->ID, 'vimeo_video_thumnbnail_url_key', $thumnbnail_url, true );
update_post_meta( $newPostID, 'video_url_id', $video_id );
You're using wp_insert_post twice which does not make sense. The first wp_insert_post is correct and will return the $post_id of the created post upon success. However, your
$newPostID = wp_insert_post($post_id);
is totally wrong and will always store 0 as a result in $newPostID, regardless of whether the post existed beforehand or not since $post_id will never contain a valid post array. What you want is to get the ID of the existing post (which post_exists already returns if successful). Change your code like so:
$existingPostID = post_exists($post_title);
if (!$existingPostID) {
$existingPostID = wp_insert_post(array(
'post_type' => 'vimeo_videos',
'post_title' => $post_title,
'post_status' => 'publish',
));
}
if ($existingPostID) {
update_post_meta( $existingPostID, 'video_url_id', $video_id );
}
For insert new post used this syntax:
add_post_meta( int $post_id, string $meta_key, mixed $meta_value, bool $unique = false )

Creating a page with wp_insert_post()

I have a form that collects data about a user's product and then creates a page in WordPress with
$my_post = array(
'post_content' => "My page content",
'post_title' => $product_title,
'post_name' => $product_title,
'post_type' => 'page', // must be 'page' to accept the 'page_template' below
'page_template' => "listing.php",
'post_status' => "publish"
);
$ID = wp_insert_post( $my_post );
$permalink = get_permalink($ID);
echo "<br />ID for new page is $ID, Permalink for new page is $permalink";
The form data is put into meta variables for the page ID and the listing.php template file pulls it out of there and builds the HTML to display the product page. This all works fine and I can see that the page meta variable, _wp_page_template, gets set correctly to the template file I specified, listing.php:
Now I want to create a second page from the same form data, this one displaying different parts of the data in a different way. So I've added a second block of code, starting at $my_cert below, that creates this second page and specifies a different template, certificate.php, that knows how to build the second version of the data.
$my_post = array(
'post_content' => "My page content",
'post_title' => $product_title,
'post_name' => $product_title,
'post_type' => 'page', // must be 'page' to accept the 'page_template' below
'page_template' => "listing.php",
'post_status' => "publish"
);
$ID = wp_insert_post( $my_post );
$permalink = get_permalink($ID);
echo "<br />ID for new page is $ID, Permalink for new page is $permalink";
$my_cert = array(
'post_content' => "My certificate", // post_content is required
'post_title' => "My certificate", // post_title is required
'post_name' => "My certificate",
'post_type' => 'page', // must be 'page' to accept the 'page_template' below
'page_template' => "certificate.php",
'post_status' => "publish"
);
$CERT_ID = wp_insert_post( $my_cert );
$cert_permalink = get_permalink($CERT_ID);
echo "<br />ID for new certificate is $CERT_ID, Permalink for new certificate is $cert_permalink";
But when I look in the meta data for the second page created, the template is set to "default" instead of certificate.php:
I know I've set up certificate.php correctly as a template (set /* Template Name: certificate */ at the top) because the Page Edit Template dropdown includes certificate:
So does anyone see why I can't create this second page with the template set to certificate.php?
Thanks
Are you sure your page template src for: certificate.php is: certificate.php? And not: templates/certificate.php or something like that. Look in your theme folder and be 100% of the page template path. Check your spelling or for typos in the page template path or name. It must be an exact match.
If you still have problems I would look into and debug the source code of: wp_insert_post()
if ( ! empty( $postarr['page_template'] ) && 'page' == $data['post_type'] ) {
$post->page_template = $postarr['page_template'];
$page_templates = wp_get_theme()->get_page_templates( $post );
if ( 'default' != $postarr['page_template'] && ! isset( $page_templates[ $postarr['page_template'] ] ) ) {
if ( $wp_error ) {
return new WP_Error('invalid_page_template', __('The page template is invalid.'));
}
update_post_meta( $post_ID, '_wp_page_template', 'default' );
} else {
update_post_meta( $post_ID, '_wp_page_template', $postarr['page_template'] );
}
}
So its probably this part that fails:
if ( 'default' != $postarr['page_template'] && ! isset( $page_templates[ $postarr['page_template'] ] ) )
Try to modify: wp-includes/post.php and go to the definition of: function wp_insert_post() on row: 2872. And add a new row on row: 3312 for debugging purposes.
echo '<pre>';
print_r( $page_templates );
echo '</pre>';
die();
Make sure your certificate.php is among those in that array. Remember to delete the debug code before continuing. This should give you some answers.

Insert some posts in a loop - wordpress plugin

I wrote a plugin, which reads csv files and create new products. The plugin works when I create only one product but when I add while in Insert() the plugin doesn't work. I want to create all products first off. Maybe it's something related to the add_action... Please help.
define( 'PLUGIN_DIR', dirname(__FILE__).'/' );
function CreateProduct($line) {
$data = explode('";"', $line);
define(POST_NAME, $data[2]);
define(cena_netto_pw, $data[5]);
$post = get_page_by_title( POST_NAME, 'OBJECT', 'product' );
$product_ID = $post->ID;
$post_data = get_post($product_ID);
function hbt_create_post() {
$my_post = array(
'post_title' => POST_NAME,
'post_content' => '',
'post_status' => 'publish',
'post_author' => 1,
'post_type' =>'product'
);
$product_ID = wp_insert_post( $my_post );
}
if(!isset($post))
hbt_create_post();
return $error_obj;
}
function Import() {
$file = PLUGIN_DIR.'test.csv';
$open = fopen($file, 'r');
while (!feof($open)) {
$line = fgets($open);
CreateProduct($line);
}
fclose($open);
}
add_action('admin_init', 'Import' );
?>
While loop code
while (!feof($open)) { $line = fgets($open); CreateProduct($line); }
This code doesn't work. It works when there is only
$line = fgets($open); CreateProduct($line);
try
fgetcsv($open)
instead
fgets($open)

insert product type from front end in wordpress

if(isset($_POST['uploadwine']))
{
global $wpdb,$woocommerce;
global $current_user;
$post_status = 'publish';
if(isset($_POST['postid']))
{
$postid=$_POST['postid'];
}else
{
$postid='';
}
$wine_post = array(
'post_title' => $_POST['wine_name'],
'post_status' => $post_status,
'post_author' => $current_user->ID,
'post_type' => 'Product',
);
if( empty($postid)){
echo $postid=wp_insert_post( $wine_post );
update_post_meta($postid, '_thumbnail_id', $_POST['aaiu_image_id'][0]);
$product_meta['year'] = $_POST['year'];
$product_meta['producer'] = $_POST['producer'];
#$product_meta['grapes_variety'] = $_POST['grapes_variety'];
$product_meta['area'] = $_POST['area'];
#$product_meta['tw_share'] = $_POST['Country'];
$product_meta['color'] = $_POST['Color'];
$product_meta['good_answer_for_the_smell_test'] = $_POST['Smell'];
$product_meta['good_answer_for_the_taste_test'] = $_POST['Taste'];
foreach($product_meta as $key =>$value)
{
update_post_meta($postid, $key, $value);
}
die;
}
I am posting product type from front end programatically. It is inserting product type into the database but not returning the post id. Instead it gives me this error "Call to a member function get_product() on a non-object in /srv/data/web/vhosts/www.wiine.me/htdocs/devbeta/wp-content/plugins/woocommerce/woocommerce-core-functions.php on line 32".
get_product() is woocommerce function may be hooked with wp_insert_post( $wine_post ) . it gives error after wp_insert_post funciton. so i can not add meta for this post.
Can anyone tell me why?

Resources