Wordpress and wp_insert_post not working - php

i'm doing an webapp that works with a wordpress website.
Until yesterday all works like a charm, but from today, when i save the post on wordpress db, it now not working...
Not give me error (php error or wordpress error), but simply a white page.
This is my full code:(sorry for many commented lines and echo, but i'm testing)
The important part:
// Create post object
$my_post = array(
'post_title' => $article_title,
'post_name' => $article_title,
'post_content' => $article_content,
'post_status' => 'pending',
'post_author' => 1
);
// Insert the post into the database
wp_insert_post($my_post, TRUE);// try first time
$post_status = "pending";
$post_type = "post";
$post_parent = "0";
$post_ID = (int) $wpdb->insert_id;
echo $post_ID; die;
Yesterday with a simple wp_insert_post(my_post); all worked fine, today (of course i think i edited some files...or i don't now), not works more and not give me error.
What do you think ?
Thank you, really!
EDIT: The problem was that i not insert post_category and wp need it!

You can capture the $post_id with the wp_insert_post() function...
$post_ID = wp_insert_post($my_post, TRUE);// try first time
Then you can dump out the $post_ID variable to get the ID or the error.
var_dump($post_ID);

add_action( 'init', 'my_func' );
function my_func() {
$my_post = '';
if( get_page_by_title($article_title,'OBJECT','post') == NULL )
$my_post= array(
'post_title' => $article_title,
'post_name' => $article_title,
'post_type' => 'post',
'post_content' => $article_content,
'post_status' => 'pending',
'post_author' => 1
);
$post_id = wp_insert_post( $my_post );
echo 'Post ID - '.$post_id;
}

Related

Create custom post type whenever another custom post type is created

I'm trying to add a feature to my Wordpress website, that every time I add a post (custom post type), another post (different custom post type) is created.
I have tried adding an action in my function file but it does not seem to work.
functions.php
function add_notification($post_id) {
global $wpdb;
$post_type = get_post_type($post_id);
if($post_type == 'exercises'){
$title = "BLA";
$post_id_new = wp_insert_post(
array(
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_title' => $title,
'post_status' => 'publish',
'post_type' => 'notification'
)
);
}
}
add_action('publish_post', 'add_notification');
I have also tried it with other hooks like new_to_publish and publish_post
With that I was expecting to have a new post (notification) when I add an exercise post.
I think it's something silly I'm forgetting but it's been like 3 days that I'm stuck in this problem.
For custom post type we have to use hook like
add_action('publish_custom-post-name','function');
In your case you have to use Like Below
function add_notification($post_id) {
global $wpdb;
$post_type = get_post_type($post_id);
if($post_type == 'exercises'){
$title = "BLA";
$post_id_new = wp_insert_post(
array(
'comment_status' => 'closed',
'ping_status' => 'closed',
'post_title' => $title,
'post_status' => 'publish',
'post_type' => 'notification'
)
);
}
}
add_action('publish_exercises', 'add_notification');
Please Try this. If any query feel free to ask.

Delete Page after plugin deactivation code not working

I am trying to delete page after plugin deactivate.The code is not working for delete page.
register_activation_hook( __FILE__, 'insert_page' );
function insert_page(){
// Create post object
$my_post = array(
'post_title' => 'Menu',
'post_content' => 'Short Code',
'post_status' => 'publish',
'post_author' => get_current_user_id(),
'post_type' => 'page',
);
// Insert the post into the database
wp_insert_post( $my_post, '' );
$newvalue = wp_insert_post( $post, false );
update_option( 'Menu', $newvalue );
}
register_deactivation_hook( __FILE__, 'deactivate_plugin' );
function deactivate_plugin() {
$page = get_page_by_title('Menu');
wp_delete_post($page);
}
I have even tried wp_delete_post($page, true);. It is also not working. Help me where i am wrong.
I'm going to address a few things if you don't mind:
First of all, you should work on your code indentation/formatting. Future you thanks you!
Second of all you're calling the wp_insert_post() function twice, but it's only working once. You can also check to see if wp_insert_post() returns a truthy value before calling update_option().
Third, be careful with "common" function names, as you risk having redeclaration errors! Try and use namespaces, classes or at the very least prefix your functions names to avoid future headaches.
Fourth, you don't need to set the $error parameter to false on wp_insert_post() as that is its default value.
Lastly, to answer your question (finally!) it's not working because wp_delete_post() requires a Post ID and you're passing a Post Object. Instead you'll want to use $page->ID. Here's your code with the changes I outlined above:
register_activation_hook( __FILE__, 'so_50960355_insert_page' );
function so_50960355_insert_page(){
// Define my page arguments
$page = array(
'post_title' => 'Menu',
'post_content' => 'Short Code',
'post_status' => 'publish',
'post_author' => get_current_user_id(),
'post_type' => 'page',
);
if( $page_id = wp_insert_post( $page ) ){
// Only update this option if `wp_insert_post()` was successful
update_option( 'my_menu_page_id', $page_id );
}
}
register_deactivation_hook( __FILE__, 'so_50960355_delete_page' );
function so_50960355_delete_page(){
$page_id = intval( get_option( 'my_menu_page_id' ) );
// Force delete this so the Title/slug "Menu" can be used again.
wp_delete_post( $page_id, true );
}

Why the value of a variable is not entering post_content on just one occasion?

I have a file of autoimport.php
$identif = (string)$numbz;
$myembed = (string)$feed_item->embed;
$feed_item->desc .= "{$identif}{$myembed}<br>{$feed_item->desc}";
$post = array(
'post_author' => '1',
'post_status' => $_POST['status'],
'post_type' => $cpt,
'post_title' => (string)$feed_item->title,
'post_content' => (string)$feed_item->desc,
);
$post_id = wp_insert_post( $post );
update_post_meta($post_id, 'sponsor', (string)$_POST['thelink']);
update_post_meta($post_id, 'contID', (string)$feed_item->id);
There are two variables that I add in post_content in the publish action, always right with manual posting, but when the plugin uses another file to do the import of the post, to trigger the post, this variable $myembed does not enter at all in post_content. Why does it happen?
I make the change in the two files, all right in the manual post.
Importmanual.php has a code almost the same as above, with difference that use post_args:
$post_args = array(
'post_author' => '1',
'post_status' => $status,
'post_type' => $tca,
'post_title' => (string)$ivd['title'],
'post_content' => (string)$ivd['desc'],
);
$post_id = wp_insert_post( $post_args );
And the names of the variables are other, but I copy and paste, there is no writing error. Include the idea is so correct that the code works and the $identif variable comes together perfectly, what could it be?
see them complete: manual post file and auto post file Within the documents search for: ‘hercules’ and you will see a short explanation before the code. https://pastebin.com/s9Xcibd6

Wordpress Plugin - Delete a Page Created when deactivating

I tried to make a custom post type plugin for my own use, and managed to make a function that creates the page for it so far. What I want to do is to delete the said page when the plugin is activated. How should the code be?
This is my code for creating the said page upon plugin activation:
function create_video_pages() {
$post = array(
'comment_status' => 'open',
'ping_status' => 'closed' ,
'post_date' => date('Y-m-d H:i:s'),
'post_name' => 'videos',
'post_status' => 'publish' ,
'post_title' => 'Videos',
'post_type' => 'page',
);
$newvalue = wp_insert_post( $post, false );
update_option( 'vidpage', $newvalue );
}
Get the post_id from your vidpage option.
Then use it to delete that post.
function deactivate_plugin() {
$page_id = get_option('vidpage');
wp_delete_post($page_id);
}
register_deactivation_hook( __FILE__, 'deactivate_plugin' );
You can do this using register_deactivation_hook and function wp_delete_post which deletes post with everything that is tied to it.
What about this?
function on_deactivating_your_plugin() {
$page = get_page_by_path( 'about' );
wp_delete_post($page->ID);
}
register_deactivation_hook( __FILE__, 'on_deactivating_your_plugin' );

wordpress create duplicate post on save / update

I know this might seem strange to some, but i would like to duplicate a post on creation.
When a post is created, i would like to duplicate it appending new data to the title, as well as updating meta fields and changing the taxonomy it is in.
Here is what i have done so far:
add_action('wp_insert_post', 'my_add_custom_fields');
function my_add_custom_fields($post_id)
{
if ( $_POST['post_type'] == 'products' ) {
$my_post = array(
'post_title' => get_the_title(),
'post_content' => '',
'post_status' => 'publish',
'post_type' => 'products',
);
$id = wp_insert_post($my_post);
update_post_meta($id,'keywords', get_the_title());
wp_set_object_terms($id, 'New Term Here', 'platform');
}
return true;
}
The issue i have is this creates an endless loop, creating the new post thousands of times and wont stop until i restart apache.
Is there away around this?
You need some sort of control for this to stop it looping. e.g. set a global value to count
$GLOBALS['control']=0;
add_action('wp_insert_post', 'my_add_custom_fields');
function my_add_custom_fields($post_id)
{
if ( $_POST['post_type'] == 'products' ) {
//if control is on third iteration dont proceed
if($GLOBALS['control']===2)
return;
//add control here!
$GLOBALS['control']++;
$my_post = array(
'post_title' => get_the_title(),
'post_content' => '',
'post_status' => 'publish',
'post_type' => 'products',
);
$id = wp_insert_post($my_post);
update_post_meta($id,'keywords', get_the_title());
wp_set_object_terms($id, 'New Term Here', 'platform');
}
return true;
}

Categories