admin管理员组文章数量:1130349
I have read other answers to similar questions but none of them solved my problem. This code works great in Editor but on Quick Edit or Bulk Edit it does not appear to fire at all. What am I doing wrong?
// link author display name to Broker Name if Author is Broker
add_action( 'save_post', 'author_is_broker', 200 );
function author_is_broker($post_id) {
// page/post options
global $lwp_options, $Listing;
$post_types = get_post_types();
unset($post_types['listings']);
$post_type = get_post_type();
//Only for listings
if(isset($post_type) && $post_type == "listings"){
// Ignore for autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
} else {
// If this is a revision, get real post ID
if ( $parent_id = wp_is_post_revision( $post_id ) )
$post_id = $parent_id;
// Get display name of post owner
$broker_id = get_post_field( 'post_author', $post_id );
$broker = get_the_author_meta('display_name', $broker_id);
// Verify directory exists for author
$args = array(
'post_type' => 'wpbdp_listing',
'author' => $broker_id
);
$wp_posts = get_posts($args);
if (count($wp_posts)) {
$is_broker = true;
} else {
return $post_id;
}
// If directory listing has been assigned, user is broker
if (isset($is_broker) && $is_broker == true) {
// add the term
$term = sanitize_text_field($broker);
$Listing->add_listing_category_term('broker', $term);
// update the post
update_post_meta( (int) $post_id, 'broker', $term );
}
else {
return $post_id;
}
}
} else {
return $post_id;
}
}
I have read other answers to similar questions but none of them solved my problem. This code works great in Editor but on Quick Edit or Bulk Edit it does not appear to fire at all. What am I doing wrong?
// link author display name to Broker Name if Author is Broker
add_action( 'save_post', 'author_is_broker', 200 );
function author_is_broker($post_id) {
// page/post options
global $lwp_options, $Listing;
$post_types = get_post_types();
unset($post_types['listings']);
$post_type = get_post_type();
//Only for listings
if(isset($post_type) && $post_type == "listings"){
// Ignore for autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
} else {
// If this is a revision, get real post ID
if ( $parent_id = wp_is_post_revision( $post_id ) )
$post_id = $parent_id;
// Get display name of post owner
$broker_id = get_post_field( 'post_author', $post_id );
$broker = get_the_author_meta('display_name', $broker_id);
// Verify directory exists for author
$args = array(
'post_type' => 'wpbdp_listing',
'author' => $broker_id
);
$wp_posts = get_posts($args);
if (count($wp_posts)) {
$is_broker = true;
} else {
return $post_id;
}
// If directory listing has been assigned, user is broker
if (isset($is_broker) && $is_broker == true) {
// add the term
$term = sanitize_text_field($broker);
$Listing->add_listing_category_term('broker', $term);
// update the post
update_post_meta( (int) $post_id, 'broker', $term );
}
else {
return $post_id;
}
}
} else {
return $post_id;
}
}
Share
Improve this question
asked Jun 15, 2018 at 21:31
ShaneShane
688 bronze badges
1 Answer
Reset to default 2It's just a guess, since I haven't tested your code, but... There is a part that looks pretty sketchy for me:
All your actions are run only if this condition is true:
if(isset($post_type) && $post_type == "listings"){
And where that $post_type variable comes from?
$post_type = get_post_type();
So you don't pass any post_id to that function call... This means that you work with global $post object. But there is no guarantee that such posts exists.
There is a reason why save_post hook passes post_id as param - you should use it inside your function...
So changing the line above to:
$post_type = get_post_type($post_id);
should solve your problem.
PS. There's no point in doing that:
$post_types = get_post_types();
unset($post_types['listings']);
You don't even use that variable in your code later on...
PPS. save_post is an action, so you don't have to return anything in it.
I have read other answers to similar questions but none of them solved my problem. This code works great in Editor but on Quick Edit or Bulk Edit it does not appear to fire at all. What am I doing wrong?
// link author display name to Broker Name if Author is Broker
add_action( 'save_post', 'author_is_broker', 200 );
function author_is_broker($post_id) {
// page/post options
global $lwp_options, $Listing;
$post_types = get_post_types();
unset($post_types['listings']);
$post_type = get_post_type();
//Only for listings
if(isset($post_type) && $post_type == "listings"){
// Ignore for autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
} else {
// If this is a revision, get real post ID
if ( $parent_id = wp_is_post_revision( $post_id ) )
$post_id = $parent_id;
// Get display name of post owner
$broker_id = get_post_field( 'post_author', $post_id );
$broker = get_the_author_meta('display_name', $broker_id);
// Verify directory exists for author
$args = array(
'post_type' => 'wpbdp_listing',
'author' => $broker_id
);
$wp_posts = get_posts($args);
if (count($wp_posts)) {
$is_broker = true;
} else {
return $post_id;
}
// If directory listing has been assigned, user is broker
if (isset($is_broker) && $is_broker == true) {
// add the term
$term = sanitize_text_field($broker);
$Listing->add_listing_category_term('broker', $term);
// update the post
update_post_meta( (int) $post_id, 'broker', $term );
}
else {
return $post_id;
}
}
} else {
return $post_id;
}
}
I have read other answers to similar questions but none of them solved my problem. This code works great in Editor but on Quick Edit or Bulk Edit it does not appear to fire at all. What am I doing wrong?
// link author display name to Broker Name if Author is Broker
add_action( 'save_post', 'author_is_broker', 200 );
function author_is_broker($post_id) {
// page/post options
global $lwp_options, $Listing;
$post_types = get_post_types();
unset($post_types['listings']);
$post_type = get_post_type();
//Only for listings
if(isset($post_type) && $post_type == "listings"){
// Ignore for autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
} else {
// If this is a revision, get real post ID
if ( $parent_id = wp_is_post_revision( $post_id ) )
$post_id = $parent_id;
// Get display name of post owner
$broker_id = get_post_field( 'post_author', $post_id );
$broker = get_the_author_meta('display_name', $broker_id);
// Verify directory exists for author
$args = array(
'post_type' => 'wpbdp_listing',
'author' => $broker_id
);
$wp_posts = get_posts($args);
if (count($wp_posts)) {
$is_broker = true;
} else {
return $post_id;
}
// If directory listing has been assigned, user is broker
if (isset($is_broker) && $is_broker == true) {
// add the term
$term = sanitize_text_field($broker);
$Listing->add_listing_category_term('broker', $term);
// update the post
update_post_meta( (int) $post_id, 'broker', $term );
}
else {
return $post_id;
}
}
} else {
return $post_id;
}
}
Share
Improve this question
asked Jun 15, 2018 at 21:31
ShaneShane
688 bronze badges
1 Answer
Reset to default 2It's just a guess, since I haven't tested your code, but... There is a part that looks pretty sketchy for me:
All your actions are run only if this condition is true:
if(isset($post_type) && $post_type == "listings"){
And where that $post_type variable comes from?
$post_type = get_post_type();
So you don't pass any post_id to that function call... This means that you work with global $post object. But there is no guarantee that such posts exists.
There is a reason why save_post hook passes post_id as param - you should use it inside your function...
So changing the line above to:
$post_type = get_post_type($post_id);
should solve your problem.
PS. There's no point in doing that:
$post_types = get_post_types();
unset($post_types['listings']);
You don't even use that variable in your code later on...
PPS. save_post is an action, so you don't have to return anything in it.
本文标签: phpAction 39savepost39 not working for quick edit
版权声明:本文标题:php - Action 'save_post' not working for quick edit 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749108009a2316901.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论