在对两个自定义字段进行筛选时,WP\\U查询无法返回帖子,尽管对每个自定义字段进行单独测试可以很好地工作。审查以下文件后custom field parameters 很明显,我还缺少一些东西。提前感谢您提供的任何帮助或想法。
不生成帖子的查询:
$query = new WP_Query(array(
\'post_type\'=>\'games\',
\'orderby\'=>\'menu_order\',
\'order\'=>\'asc\',
\'posts_per_page\' => 1,
\'meta_query\'=> array(
\'relation\'=>\'AND\',
array(
\'key\'=>\'game_date_time\',
\'value\'=>$nowtoday,
\'compare\' =>\'>\'
),
array(
\'key\'=>\'home_team\',
\'value\'=>\'Allen\',
\'compare\'=>\'LIKE\'
)
)
));
生成的SQL:
`SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) INNER JOIN wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id) WHERE 1=1 AND wp_posts.post_type = \'games\' AND (wp_posts.post_status = \'publish\' OR wp_posts.post_status = \'private\') AND ( (wp_postmeta.meta_key = \'game_date_time\' AND CAST(wp_postmeta.meta_value AS CHAR) > \'3/5/2014 23:12\') AND (mt1.meta_key = \'home_team\' AND CAST(mt1.meta_value AS CHAR) LIKE \'%Allen%\') ) GROUP BY wp_posts.ID ORDER BY wp_posts.menu_order asc LIMIT 0, 1`
风险值
$nowtoday
是一个格式化的时间戳,用于与自定义字段进行比较
game_date_time
. 仅在上查询
game_date_time
产生预期结果。这是该查询,它将按预期返回下一个按时间顺序排列的游戏(主场或客场)。
$query = new WP_Query(array(
\'post_type\'=>\'games\',
\'orderby\'=>\'menu_order\',
\'order\'=>\'asc\',
\'posts_per_page\' => 1,
\'meta_query\'=> array(
\'relation\'=>\'AND\',
array(
\'key\'=>\'game_date_time\',
\'value\'=>$nowtoday,
\'compare\' =>\'>\'
),
array(
/* \'key\'=>\'home_team\',
\'value\'=>\'Allen\',
\'compare\'=>\'LIKE\' */
)
)
));
生成的SQL:
`SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) WHERE 1=1 AND wp_posts.post_type = \'games\' AND (wp_posts.post_status = \'publish\' OR wp_posts.post_status = \'private\') AND ( (wp_postmeta.meta_key = \'game_date_time\' AND CAST(wp_postmeta.meta_value AS CHAR) > \'3/5/2014 23:11\') ) GROUP BY wp_posts.ID ORDER BY wp_posts.menu_order asc LIMIT 0, 1`
类似地,修改后的查询只返回home games,也就是说,其中home\\u game类似于“Allen”,将返回预期结果。
$query = new WP_Query(array(
\'post_type\'=>\'games\',
\'orderby\'=>\'menu_order\',
\'order\'=>\'asc\',
\'posts_per_page\' => 1,
\'meta_query\'=> array(
\'relation\'=>\'AND\',
array(
/* \'key\'=>\'game_date_time\',
\'value\'=>$nowtoday,
\'compare\' =>\'>\' */
),
array(
\'key\'=>\'home_team\',
\'value\'=>\'Allen\',
\'compare\'=>\'LIKE\'
)
)
));
生成的SQL:
`SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id) WHERE 1=1 AND wp_posts.post_type = \'games\' AND (wp_posts.post_status = \'publish\' OR wp_posts.post_status = \'private\') AND ( (wp_postmeta.meta_key = \'home_team\' AND CAST(wp_postmeta.meta_value AS CHAR) LIKE \'%Allen%\') ) GROUP BY wp_posts.ID ORDER BY wp_posts.menu_order asc LIMIT 0, 1`
令人困惑的是,当有许多帖子同时满足这两个条件时,为什么这两个过滤器一起不会产生结果,
game_date_time > $nowtoday AND home_team LIKE \'Allen\'
.
感谢您的时间和对解决方案的任何建议!