WordPress/Timber - 错误:wpdb::prepare() 的查询参数必须有一个占位符

时间:2021-06-13 10:45:14

标签: php sql wordpress

我使用 Timber (TWIG) 和 WordPress。

我正在尝试使用第一个帖子标题字母/数字创建一个字母数字过滤器。

A - B - C - D .... [0-9]

选择“A”时的结果示例:

  • 安纳托尔
  • 阿拉斯
  • 阿尔忒弥斯

$context['alpha'] 等于“num”时,出现错误:

wpdb::prepare 被错误地调用。 wpdb::prepare() 的查询参数必须有一个占位符。

$context['alpha'] = 'num';

$postids = [];

if ($context['alpha']) {
    if ($context['alpha'] == "num") {
        $postids = $wpdb->get_col($wpdb->prepare("
            SELECT      ID
            FROM        $wpdb->posts
            WHERE       $wpdb->posts.post_title regexp '^[0-9]'
            AND         $wpdb->posts.post_type = 'exhibitors'
            ORDER BY    $wpdb->posts.post_title"));
    } else {
        $postids = $wpdb->get_col($wpdb->prepare("
            SELECT      ID
            FROM        $wpdb->posts
            WHERE       SUBSTR($wpdb->posts.post_title,1,1) = %s
            AND         $wpdb->posts.post_type = 'exhibitors'
            ORDER BY    $wpdb->posts.post_title",$context['alpha']));
    }
}

$list_exhibitors_complete = Timber::get_posts(array(
    'post__in' => $postids,
    'post_type' => 'exhibitors',
    'post_status' => 'publish',
    'orderby' => 'title',
    'order' => 'ASC',
    'posts_per_page' => -1
));

请问怎么了?

0 个答案:

没有答案