In WordPress with the publish_{post_type} (http://codex.wordpress.org/Post_Status_Transitions) you can hook into the loop if a post with custom type is published. But publish_{post_type} is also triggers if a a post is updated.
I am basically looking for a way to check the old and new status, within the publish_post_type hook. Does anyone have a nifty idea as to how to accomplish that?
I am doing this:
function doStuff( $post_ID ) {
// do stuff
return $post_ID;
}
add_action( 'publish_ttplaned', 'doStuff' );
So basically I need to check the old and new status of the post within the fuction doStuff().
In case someone else is looking for the aswer. I ended up using Williams solution.
add_action( 'draft_to_publish', 'doStuff', 10, 1 );
add_action( 'pending_to_publish', 'doStuff', 10, 1 );
function doStuff( $post ) {
if ( get_post_type( $post ) == "my_custom_post_type" ){
// do stuff
}
}
This works since a new post starts out as a draft even if you publish it right away.
As #rnevius says, there's no hook in WordPress core called publish_post_type.
There is also publish_post - which I've just tested it to make sure it only runs when the post is first published, not when it's subsequently edited.
So you can do something like:
function my_function($post_ID, $post) {
if ("foo" == $post->post_type) {
// do something
}
}
add_action( 'publish_post', 'my_function', 10, 2 );
There is also the wp_transition_post_status(), which calls several actions:
transition_post_status with $new_status, $old_status, $post
or, if you want to do it the other way around:
{$old_status}_to_{$new_status} and passes a WP_Post object as the only parameter.
Related
i want to call my custom function on Bulk order status changed, to add some additional functionality.
for that i tried following hooks but nothing work..
add_action( 'admin_action_woocommerce_order_status', 'bulk_order_my_function' );
add_action( 'admin_action_woocommerce_order_status_processing', 'bulk_order_my_function' );
add_action( 'admin_action_wc_processing', 'bulk_order_my_function' );
add_action( 'admin_action_woocommerce_processing', 'bulk_order_my_function' );
i am more interested in general order status hook for bulk order update instead of creating separate function for each status..
i.e: so in my one function bases on IF/ELSE condition i can put my logic.
Would be grateful if anyone can help?
So i downloaded the whole woocommerce package and find the string bulk in the woocommerce folder and certainly i found the hook in the below mentioned file:
\woocommerce\includes\admin\list-tables\abstract-class-wc-admin-list-table.php
hook we can use is
add_filter( 'handle_bulk_actions-edit-shop_order', 'bulk_order_stock_update', 10, 3 );
function will be some thing like following
function bulk_order_stock_update($redirect_to, $action, $ids) {
if ( false !== strpos( $action, 'mark_' ) ) {
$new_status = substr( $action, 5 ); // Get the status name from action.
$report_action = 'marked_' . $new_status;
//place your some condition here....
}
}
I am trying to include a function after an order is updated (admin side), but it doesn´t work. I want to call a function when someone update an order (order status, etc...). I am trying this:
add_action( 'woocommerce_process_shop_order_meta', 'woocommerce_process_shop_order', 1, 1);
function woocommerce_process_shop_order () {
// Code
}
Ive been trying to redirect to other web with header("Location: www.example.com"), but the page doesn´t redirect when an order is update :(
Please, can you help me to solve it?
Thanks!
There you go. Tested and works. Goes into functions.php file of your child theme:
add_action( 'post_updated', 'post_updated_action', 20, 3 );
function post_updated_action( int $post_id, WP_Post $post_after, WP_Post $post_before ): void {
// To be sure it gets called when updated from the admin dashboard and is order
if ( $post_before->post_type !== 'shop_order' || ! is_admin() ) {
return;
}
wp_safe_redirect( home_url() );
exit;
}
I am developing a plugin and need the list of post types existing in the WordPress backend, I checked the WP_Query page of WordPress codex but could not find the solution
You can try this code.
<?php
// hook into init late, so everything is registered
// you can also use get_post_types where ever. Any time after init is usually
fine.
add_action( 'init', 'wpse34410_init', 0, 99 );
function wpse34410_init()
{
$types = get_post_types( [], 'objects' );
foreach ( $types as $type ) {
if ( isset( $type->rewrite->slug ) ) {
// you'll probably want to do something else.
echo $type->rewrite->slug;
}
}
}
i think it will work for you
Is there any way to be registered to wordpress events?
I want to know when a post was updated or created in the system, and get its ID.
Is it possible?
Thanks
You can use the save post hook. It is triggered when the post is created and updated.
Here's a sample.
function post_hook( $post_id ) {
if ( wp_is_post_revision( $post_id ) ){
//Post is being updated
} else {
//Post is being created
}
}
add_action( 'save_post', 'post_hook' );
Here's a link from the codex.
Save Post WP
I'm developing a plugin and one of the issues I am running into is that I cannot get the post id within a function assigned to the admin_init hook.
I tried a few different methods; but, they all seem to use the $wp_query.
Below is a simple version the code I am using. I implemented the code like this just now and ran it by viewing the "post edit" page
add_action('admin_init','do_optional_featured_article');
function do_optional_featured_article()
{
global $wp_query;
echo "<pre>";
print_r($wp_query);
echo "</pre>";
die();
}
$wp_query is a mostly empty array, notably, the post member is empty
-- EDIT --
I got some advice over at wordpress.stackexchange and added in this function:
function get_admin_post()
{
if( isset($_GET['post']) )
{
$post_id = absint($_GET['post']); // Always sanitize
$post = get_post( $post_id ); // Post Object, like in the Theme loop
return $post;
}
elseif( isset($_POST['post_ID']) )
{
$post_id = absint($_POST['post_ID']); // Always sanitize
$post = get_post( $post_id ); // Post Object, like in the Theme loop
return $post;
}
else
{
return false;
}
}
I think this answer will help. It states that the earliest action you can hook into, to get the global $post/$posts variables is the wp action. In the action hook reference on the codex, you can see that the wp action executes a bit after admin_init, which is why you can't retrieve any posts there, I think.
So, that should work:
add_action('wp','do_optional_featured_article');