doulie0178 2015-08-25 06:12
浏览 32
已采纳

从WordPress中的p2p_id获取from和to对象 - WP Posts to Posts Plugin

I'm now using WP Posts to Posts plugin in wordpress.

And now I made some links between posts and posts, users and users, or posts and users.

And in all three cases above, I want to do more with the p2p plugin.

For example, I may fetch the p2p_id first:

$users = get_users( array(
    'connected_type' => 'multiple_authors',
    'connected_items' => $post
) );

foreach($users as $user) {
    $p2p_id = $user->p2p_id;

    // ********** ATTENTION *********
    // Here, I got the p2p_id of the p2p object
    // In a general purpose, I want to get the 
    // from and to object from the p2p_id

}

So, how can I get the from and to object via the p2p_id? I've found over the documentation, but seemed no effective ways.

  • 写回答

1条回答 默认 最新

  • doushi7761 2015-08-26 00:57
    关注

    No other answers. So I finally found the answer myself from the source code:

    File: /wp-content/plugins/posts-to-posts/vender/scribu/lib-posts-to-posts/api.php

    Notice that there is a function: p2p_get_connection($p2p_id)

    Then we call that function with a known p2p_id, an stdClass Object is returned, like the below:

    object(stdClass)#2509 (4) {
      ["p2p_id"]=>
      string(1) "8"
      ["p2p_from"]=>
      string(2) "84"
      ["p2p_to"]=>
      string(1) "2"
      ["p2p_type"]=>
      string(10) "my_post_to_user"
    }
    

    Now that we can get the p2p_from id and p2p_to id, and we know it is a post or an user. We can construct the object.

    So the finally solution may look like:

    $conn = p2p_get_connection($this->p2p_id);
    $from = get_post(intval($conn->p2p_from));
    $to = new WP_User(intval($conn->p2p_to));
    

    Still seemed not found in the documentation, hope it helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab实现基于主成分变换的图像融合。
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊