【问题标题】:Add custom post type title to an array将自定义帖子类型标题添加到数组
【发布时间】:2020-05-06 20:32:16
【问题描述】:

我正在尝试更改找到here 的插件。这是一个关于组装插件的 Twillio 教程,该插件在使用他们的系统“发布”WordPress 帖子时发送 SMS。我面临的障碍是每天都会生成很多帖子,我不希望多条短信发出。那会很烦人。因此,我创建了一个名为“SMS”的自定义帖子类型。我无法将描述的插件配置为使用自定义帖子类型,而不是初始配置中提供的默认或“帖子”wordpress。

// Prepares SMS to be sent when post is published

// function post_published_notifications($ID, $post ) //Original plugin function. I'm unsure of how to reassign global $post

function post_published_notification($ID) // I added this trying to access CPT, sans $post
  {

    $sid = '####################';
    $token = '######################';
    $from = '+1530#######';
    $client = new Client($sid, $token);

    $gallery_args = array(
      'post_type'=> 'SMS',
    );

    $posts_display_gallery = get_posts( $gallery_args ); // My code that doesn't work. Trying to access the CPT named SMS

    $title = $posts_display_gallery->post_title; // My code that doesn't work. Trying to assign variable to CPT title.

    //$title = $post->post_title; // gets post title. Provided by Twillio. Returns post title.

    $body = sprintf('New Post: %s', $title);

    $blogusers = get_users('blogid=$ID&role=subscriber');
    foreach ($blogusers as $user) {
      $to = get_user_meta($user->ID, 'mobile', true);
      if (intval($to) == 0) {
        continue;
      }
      $client->messages->create(
        $to,
        array(
          'from' => $from,
          'body' => $body  // Custom post type title should be held here.
        )
      );
    }
  }

  add_action('publish_post', 'post_published_notification', 10, 2);


【问题讨论】:

  • 如果post_title 属性正确,你能检查$posts_display_gallery 吗?

标签: php wordpress custom-post-type


【解决方案1】:

看看 WordPress 专门绑定到帖子类型的钩子系统,这里是他们相关文档的链接:

https://developer.wordpress.org/reference/hooks/save_post_post-post_type/

【讨论】:

  • 谢谢 Aran.. 尝试了几种使用 get_post_type() 获取 SMS CPT 的不同方法,甚至将我的 SMS 代码包装在条件中.. 但我没有得到它..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-19
  • 2013-08-04
  • 1970-01-01
  • 2019-09-12
  • 1970-01-01
  • 2017-04-21
  • 1970-01-01
相关资源
最近更新 更多