【问题标题】:How to get Post ID in Wordpress Admin如何在 Wordpress Admin 中获取 Post ID
【发布时间】:2012-01-17 18:52:30
【问题描述】:

我正在开发一个 Wordpress 插件,我需要获取当前的帖子 ID
写帖子/写页面编辑屏幕(循环外)。

我还需要在 "admin_print_scripts" 钩子之前执行此操作,因为我想将一些数据传递给 javascript 文件。

我不能使用:

$id = $_GET['post'];

因为当您添加新帖子或页面时,url 不包含此变量。

到目前为止,我已经尝试了这些选项,但都没有奏效:

A) 这将返回 ID 0

function myplugin_setup() {
    global $wp_query;
    $id = $wp_query->get_queried_object_id();
    var_dump($id);
}

add_action('admin_init', 'myplugin_setup' );  

B) 这将返回一个空 ID

function myplugin_setup() {
    global $wp_query;
    $id = $wp_query->post->ID;
    var_dump($id);
}

add_action('admin_init', 'myplugin_setup' );

C) 这也返回一个空 ID

function myplugin_setup() {
    global $post;
    $id = $post->ID;
    var_dump($id);
}

add_action('admin_init', 'myplugin_setup' );

【问题讨论】:

标签: php wordpress


【解决方案1】:
$post_id = null;
if(isset($_REQUEST['post']) || isset($_REQUEST['psot_ID'])){
    $post_id = empty($_REQUEST['post_ID']) ? $_REQUEST['post'] : $_REQUEST['post_ID'];  
}

我认为它会帮助您在管理面板中获取 id。

【讨论】:

    【解决方案2】:

    对于仍然想知道要调用的正确钩子或 wordpress 管理生命周期中的众多钩子之一的任何人,是:admin_head

    如下:

    function myplugin_setup() {
        global $post;
        $id = $post->ID;
        var_dump($id);
    }
    
    add_action('admin_head', 'myplugin_setup' );
    

    【讨论】:

      【解决方案3】:

      当用户访问管理区域时,'admin_init' 操作在任何其他挂钩之前触发。它在新帖子获得 id 之前触发。

      要获取新的帖子 ID,您可以使用“save_post”,这是在创建或更新帖子或页面时触发的操作 (http://codex.wordpress.org/Plugin_API/Action_Reference/save_post)。

      首先,您可以使用“admin_enqueue_scripts”包含您的脚本,然后使用“save_post”获取新的帖子 ID。 'admin_print_scripts' 在 'save_post' 之后触发,您可以使用 wp_localize_script (https://codex.wordpress.org/Function_Reference/wp_localize_script) 或其他方法将新的帖子 ID 传递给您的 javascript。

      我需要类似的东西,但它是在课堂上使用的。

      class Foo {
          // this will hold the id of the new post
          private $postId = null;
      
          public function __construct()
          {
              // the actions are triggered in this order
              add_action('admin_enqueue_scripts', array($this, 'EnqueueScripts'));
              add_action('save_post', array($this, 'SavePost'));
              add_action('admin_print_scripts', array($this, 'LocalizeScripts'));
          }
      
          // enqueue your scripts and set the last parameter($in_footer) to true
          public function EnqueueScripts()
          {
              wp_enqueue_script('myJs', 'js/my.js', array('jquery'), false, true); 
          }
      
          // use wp_localize_script to pass to your script the post id
          public function LocalizeScripts()
          {
              wp_localize_script('myJs', 'myJsObject', array('postId'=>$this->GetPostId()));
          }
      
          // if $post_id is different from global post id you are in the write/create post page, else you are saving an existing post
          public function SavePost($post_id)
          {
              if($post_id != $this->GetPostId())
              {
                  $this->SetPostId($post_id);
              }
          }
      
          private function GetPostId()
          {
              if (!$this->postId) {
                  global $post;
                  if($post)
                  {
                      $this->SetPostId($post->ID);
                  }
              }
      
              return $this->postId;
          }
      
          private function SetPostId($postId)
          {
              $this->postId = $postId;
          }
      }
      

      现在,您可以在 javascript 中使用:

      myJsObject.postId
      

      【讨论】:

        【解决方案4】:

        这可能有效:

        $id = get_the_ID();
        

        【讨论】:

          【解决方案5】:

          确保在 WordPress 查询之后调用全局 $post。如果您向 init 或 admin_init 添加操作,则查询尚未准备好,因此您无法从全局 $post 变量中获取任何信息。

          我的建议是查看此页面中的操作参考:http://codex.wordpress.org/Plugin_API/Action_Reference,然后选择适合您的。

          例如,我是这样做的:

          add_action( 'admin_head', 'check_page_template' );
          function check_page_template() {
              global $post;
              if ( 'page-homepage.php' == get_post_meta( $post->ID, '_wp_page_template', true ) ) {
                  // The current page has the foobar template assigned
                  // do something
          
              }
          }
          

          我能够在 WP admin 中获取页面 ID

          【讨论】:

            【解决方案6】:

            问题是,您正在使用 admin_init 挂钩。如果您查看操作参考 - http://codex.wordpress.org/Plugin_API/Action_Reference - 您会看到,这个钩子实际上是在查询帖子之前调用的,这就是您使用的那些变量尚未填充的原因。

            您可以使用一些稍后的操作(通过一些 is_admin() 检查),或者您可以使用 admin init 钩子将操作添加到稍后的钩子中,所以它只会在管理员上使用。

            【讨论】:

              【解决方案7】:

              用途:

              global $post
              

              在函数的开头。然后,您应该可以访问 $post->ID 以获取当前帖子的 id。这适用于新帖子和现有帖子。

              【讨论】:

              • 参见@Cippo answer,使用global $post 的函数/方法需要在$post 可用后挂钩,将函数挂钩到admin_head 有效
              【解决方案8】:

              如果是新帖子/页面,我猜 id 还不存在,因为该帖子尚未发布/添加到数据库。如果您正在尝试编辑帖子/页面,我认为您可以使用$id = $_GET['post'];

              【讨论】:

              • 实际上,每次您请求新的帖子/页面屏幕时,wordpress 都会在数据库中插入一个帖子状态为“自动草稿”的新帖子。
              • 在这种情况下如何获取自动草稿帖子ID?我需要为新帖子预定义一些帖子元值。
              • @Ciantic 您可以像获取普通帖子一样获取自动草稿的 ID。每次打开“添加帖子/页面”时都会在数据库中创建一条新记录,然后页面以空值呈现,因此从技术上讲,在页面呈现时您将拥有一个标准的全局 $post 变量。
              • @Ciantic 您可能对 the_post 操作感兴趣:codex.wordpress.org/Plugin_API/Action_Reference/the_post - 但是在您的回调中,您应该检查它是否是新创建的页面:global $pagenow; $newMode = $pagenow == 'post-new.php';
              猜你喜欢
              • 2023-01-18
              • 1970-01-01
              • 2019-08-01
              • 1970-01-01
              • 2013-07-10
              • 1970-01-01
              • 2017-04-14
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多