I am creating new post using wp_insert_post
inside function.php. All the posts are created fine. I use FS POSTER plugin to share published article on social automatically. When I created posts using wp_insert_post
, FS POSTER does not share the post.
But when I create and publish post manually, it will share the post automatically.
How to notify other plugins like fs poster or any other social share plugins when I create the post using wp_insert_post
.
if I create post in function.php, with post statusdraft
and then publish it using UI publish button, it will share automatically.
How to fix this, what I am missing here ?
the plugin you use probably does not listen to newly createdwp_insert_post
and its related hook, I can only guess it only runs ifwp_insert_post
is called to update a record.
The thing is when you create a post in admin panel there are multiple actions that runs and you are not creating a database record when you hit save. when you type in the Post Title, this automatically creates a record in posts table withdraft
post_status
and when you hit Save/Publish, you are actually updating the database record and not creating a new record that runs multiple action hooks.
On the other hand,wp_insert_post
directly create a database record in a single action.
You can try something like setting the post status into a custom status,
i.e.
// Create a post, change the action hook or however you create the post
add_action('how-ever-you-invoke-post-creation', function() {
$post = array(
'post_title' => 'My not so cool post',
'post_type' => 'post',
'post_status' => 'waiting'
);
$postID = wp_insert_post( $post );
});
the code above will create a posts record in your database and sets itspost_status
towaiting
. just a note: you won't be able to see posts with custompost_status
in admin dashboard
now, try publishing your post after its created usingwp_after_insert_post
add_action('wp_after_insert_post', function( $postID, $post ) {
if ( $post && $post->post_status === 'waiting') // only run if post status is waiting
wp_publish_post( $post );
}, 10, 2);
If this still doesn't trigger your plugin related functionality, then they also did not listen for thatwp_publish_post
and all the related hooks that runs with it.
Your best bet would be to install query monitor and monitor the hooks that runs or dig in the plugin code yourself.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.