admin管理员组

文章数量:1414621

How can I get correct action hook on publish_posts for multiple custom types ?

I have a following problem, I can use this hook for posts or single custom post_type, but I don't know correct hook for multiple custom post types:

// Post publication for posts
add_filter ( 'publish_post', 'notify_published_post' );
function notify_published_post( $post_id ) {

I would like to publish to all even future custom post_types, so I would like to get something like this:

// Post publication hook for all custom posts
add_filter ( 'publish_anypost', 'notify_published_post' );
function notify_published_post( $post_id ) {

How can I get correct action hook on publish_posts for multiple custom types ?

I have a following problem, I can use this hook for posts or single custom post_type, but I don't know correct hook for multiple custom post types:

// Post publication for posts
add_filter ( 'publish_post', 'notify_published_post' );
function notify_published_post( $post_id ) {

I would like to publish to all even future custom post_types, so I would like to get something like this:

// Post publication hook for all custom posts
add_filter ( 'publish_anypost', 'notify_published_post' );
function notify_published_post( $post_id ) {
Share Improve this question edited Oct 16, 2013 at 7:45 Ravinder Kumar 2,5681 gold badge14 silver badges20 bronze badges asked Jul 29, 2013 at 17:38 Daniel FoltynekDaniel Foltynek 1836 silver badges21 bronze badges 3
  • Does publish_post not work for custom post types? – montrealist Commented Jul 29, 2013 at 17:52
  • It works but for only one, I can make it work for publish_portfolio for example, but I would like to make it work for all custom post types I have. Or did you mean if it doesn't work for all of them in default? No, I have tried it and it didn't work for custom post types. – Daniel Foltynek Commented Jul 29, 2013 at 17:56
  • i think question will contain add_action in example code instead of add_filter – Ravinder Kumar Commented Jul 29, 2013 at 18:10
Add a comment  | 

2 Answers 2

Reset to default 2

I think you can achieve this by:

1.determine the custom post types by using get_post_types() fx

It returns a list of post names or objects on basis of parameter you pass

<?php
$args = array(
  'public'   => true,
  '_builtin' => false
); //pass parameter to array according to get all custom post types( parameter pass for demo only.modify it to get desire result)
$post_types=get_post_types($args,'names');

?>

2.add action to custom post type when publish

<?php
foreach($post_types as $post_type){
  add_action( 'publish_'.$post_type, 'ravs_notify_published_post' );
}
function ravs_notify_published_post( $post_id ) {
 $post = get_post( $post_id );
 // Here's the magic
 Wp_Heartbeat_Notify::notify( array(
    'title'     =>      'New Post by ' . $post->post_author,
    'content'   =>      'There\'s a new post publish, why don\'t you <a href="' .get_permalink($post_id). '">give it</a> a look?',
    'type'      =>      'info'
  ));
}
?>

Updated

Create a plugin to show realtime notifications WP-Realtime-Notify

Edit

Paste this code in functions.php.It'll print array of name of custom post types.goto get_post_types and see all parameters.pass correct parameters which gives you required output( change $args ).if you get correct array of name of custom post types for required $args,change it with my $args in plugin.

<?php
add_action('the_content','ravs_customPostList');
function ravs_customPostList(){
$args = array(
  'public'   => true,
);
$post_types = get_post_types($args,'names'); //get names of post types
print_r($post_types);
}?>

As i wrote on the doc this plugin has been full tested on WordPress 3.6-beta3 both on localhost and online but i'm not sure how it would work on later RC.

As soon as 3.6 release is coming out i'll check it out. For testing purpose i would suggest to download beta 3 here: http://wordpress/wordpress-3.6-beta3.zip

Remember that to see realtime notification you have to open two browser cause events are obviously not shown to the user that triggered them.

Hope this can help :)

本文标签: hooksHow to create notification on frontend using heartbeat api for multiple custom post types