【问题标题】:$wpdb->get_results issue$wpdb->get_results 问题
【发布时间】:2012-05-15 01:28:48
【问题描述】:

我有一个 $wpdb->get_results 查询,如下所示:

"SELECT *
 FROM $wpdb->posts
 WHERE post_author = $current_user_id
   AND post_status = 'publish'
   AND post_type = 'event'
 ORDER BY post_date DESC"

翻译成:

SELECT *
FROM wp_posts
WHERE post_author = 5
  AND post_status = 'publish'
  AND post_type = 'event'
ORDER BY post_date DESC

如果我通过 phpmyadmin 在数据库中运行该精确查询,它返回的结果非常好。但是在 wordpress 实现中它不返回任何内容。 我已将其缩小到似乎正在破坏它的AND post_type = 'event' 部分。当我离开查询时,它返回结果就好了

有人知道为什么吗?

谢谢!

【问题讨论】:

  • 出于好奇,您是否有一个名为“事件”的注册自定义帖子类型?您的数据库中是否有任何带有 post_type 事件的条目?
  • 你确定 $current_user_id = 5 吗?您确定要针对同一个数据库运行这两个查询吗?
  • 我也遇到了同样的问题,你找到解决办法了吗?
  • phpMyAdmin 可能会通过添加LIMIT 0, 30 来修改查询,但它不应该产生这样的差异。

标签: mysql wordpress


【解决方案1】:

据我了解,wordpress 有 5 种主要的帖子类型:(帖子、页面、附件、修订和 nav_menu_item)

要查找“事件”的 post_type,您需要使用 register_post_typefunction 注册此自定义帖子类型。

这是一个示例,说明如何创建名为“事件”的自定义帖子类型

add_action( 'init', 'create_post_type' );
function create_post_type() {
    register_post_type( 'event',
        array(
            'labels' => array('name' => __( 'events' ),
                    'singular_name' => __( 'event' )),
            'public' => true,
        )
    );
}

注意:register_post_type($post_type, $args) 默认采用 2 个参数。

$post_type - 是您的自定义帖子类型的名称。

$args - 是用于修改帖子类型默认行为的任意数量的参数。这通常应该是一个数组。

'labels' 参数以一般和单数形式描述帖子类型的名称。如果您更喜欢帖子类型的通用版本和单数版本,请使用所示方法。否则,默认值就是您提供的 $post_type name 名称。

'public' 参数表示帖子类型是否旨在供公众使用。

你也可以写一个不带很多标签的简化版如:

add_action( 'init', 'create_post_type' );
function create_post_type() {
    register_post_type( 'event', array( 'public' => true, 'label' => 'events' ) );
}

另请注意,create_post_type 是函数的自定义名称,您可以将其称为 my_super_cool_new_custom_original_post_type_creating_function 哈哈,或者根据您的需要进行描述。

在简化版本中,我使用了label,它接受一个参数,这是帖子类型的通用名称。如果您想为您的帖子类型添加描述,您可以在名称的括号内进行,并将一个下划线替换为 x,例如:

add_action( 'init', 'create_post_type' );
register_post_type( 'event',
    array(
        'labels' => array('name' => _x( 'events' , 'post type general name' ),
                'singular_name' => _x( 'event' , 'post type singular name' )),
        'public' => true,
    )
);

注意事项:不要在初始化之前使用register_post_type

现在,如果您已完成所有这些操作并且无法访问 post_type,请确保您已向其中添加了 public 参数。 :)

干杯

【讨论】:

    【解决方案2】:

    我遇到的问题来自新线路。由于某种原因,编辑器或数据库配置错误,因为它从未发生在我身上。

    一旦我将查询更改为在同一行上,它就开始工作了。

    【讨论】:

      【解决方案3】:

      试试下面的代码:

      $get_post = $wpdb->query("SELECT * FROM wp_posts
      WHERE post_author = ".$current_user_id."
        AND post_status = 'publish'
        AND post_type = 'event'
      ORDER BY post_date DESC
      "); 
      

      $wpdb->get_results("SELECT *
       FROM $wpdb->posts
       WHERE post_author = $current_user_id
         AND post_status = 'publish'
         AND post_type = 'event'
       ORDER BY post_date DESC", OBJECT);
      

      【讨论】:

        【解决方案4】:

        尝试使用post_type like %event%

        您可以通过 WP_Query query_posts 执行相同的操作,这不是一个需要使用 $wpdb 的复杂查询,但我可能错了。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-11-17
          • 1970-01-01
          • 1970-01-01
          • 2019-05-01
          • 2016-02-13
          相关资源
          最近更新 更多