Mysql $wpdb->;获取结果问题

Mysql $wpdb->;获取结果问题,mysql,wordpress,Mysql,Wordpress,我有一个$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_s

我有一个$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实现中,它不会返回任何内容。 我已经把它缩小到了
和post_type='event'
这两个似乎正在破坏它的部分。当我把它从查询中去掉时,它会很好地返回结果

有人知道为什么吗


谢谢

尝试使用
post\u类型,如%event%

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

请尝试以下代码:

$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
"); 


我遇到的问题来自新的生产线。出于某种原因,编辑器或数据库配置错误,因为我从未遇到过这种情况


一旦我将查询更改为与开始工作时相同的行。

据我所知,wordpress有5种主要的帖子类型:(帖子、页面、附件、修订版和导航菜单项)

要查找“事件”的帖子类型,您需要使用
register\u post\u type
功能注册此自定义帖子类型

下面是如何创建名为“事件”的自定义帖子类型的示例

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\u post\u type($post\u type,$args)
默认采用2个参数

$post\u type
-是自定义帖子类型的名称

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

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

“public”
参数表示帖子类型是否用于公共用途

您还可以编写简化版本,而不需要许多标记,例如:

add_action( 'init', 'create_post_type' );
function create_post_type() {
    register_post_type( 'event', array( 'public' => true, 'label' => 'events' ) );
}
还要注意的是,
create\u post\u type
是一个函数的自定义名称,您可以调用该函数的
my\u super\u cool\u new\u custom\u original\u post\u type\u creating\u function
haha或一些描述您需要的东西

在简化版中,我使用了
label
,它接受一个参数,这是post类型的通用名称。如果要为帖子类型添加描述,可以在名称的括号内添加,并将一个下划线替换为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,
    )
);
注意事项:在初始化之前,不要使用
寄存器\u post\u type

现在,如果您已经完成了所有这些操作,并且无法访问post_类型,请确保已将public参数添加到该类型中。:)


干杯

出于好奇,您是否有名为“事件”的注册自定义帖子类型?您的数据库中是否有post_类型事件的条目?您确定$current_user_id=5吗?您确定要对同一个数据库运行两个查询吗?我有同样的问题,您找到解决方案了吗?phpMyAdmin可以通过向查询中添加
LIMIT 0,30
来修改查询,但不应该产生这样的差异。
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,
    )
);