【问题标题】:How to retrieve custom post from custom database table in wordpress?如何从 wordpress 中的自定义数据库表中检索自定义帖子?
【发布时间】:2015-05-05 07:02:15
【问题描述】:

我正在开发一个 wordpress 插件。我在其中创建了名为部门的自定义帖子。此外,我已将所有部门的帖子存储在名为 awr_departments 表的自定义表中。现在我想从我的自定义表 awr_departments 中检索仪表板中的所有部门帖子,而不是从内置表 wp_posts 中检索。我知道数据库查询,但不知道为在仪表板中列出自定义帖子而触发的钩子或过滤器。请指导我完成这项任务。

注意:我无法更改核心 wordpress 文件,因为我想要插件本身的所有功能。

这是我在自定义表格中插入自定义帖子的代码。

function save_awr_details(){
global $post;
  global $wpdb;

   if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
      return;

 if ( get_post_type($post) == 'awr_department'){  

  // $post->post_status='publish';
   if(isset($post->post_title)) {
      $check=$wpdb->insert( 
                    'awr_departments', 
                    array( 
                      'title' => $_POST['post_title'],
                    )
                  );
   }
 }
}
add_action('publish_awr_department', 'save_awr_details');

【问题讨论】:

    标签: wordpress


    【解决方案1】:

    如果你想在 wordpress 仪表板 上显示所有结果,你必须创建一个 widget 来为此创建一个小部件,然后使用这个钩子

    add_action('wp_dashboard_setup', 'yourcustom_dashboard_widget');
    

    【讨论】:

      【解决方案2】:

      您可以使用它从数据库中检索数据。

       $query = new WP_Query( array(
          'post_type' => 'awr_department', //your post name
          'posts_per_page' => -1,
          'order' => 'ASC',
          'orderby' => 'title',
      ) );
      

      【讨论】:

        猜你喜欢
        • 2015-09-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多