WP_QUERY和两个定制字段不返回帖子

时间:2014-03-06 作者:jdm2112

在对两个自定义字段进行筛选时,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\'.

感谢您的时间和对解决方案的任何建议!

1 个回复
最合适的回答,由SO网友:jdm2112 整理而成

第一个查询提供更正日期格式时的预期结果。

$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\'
) ) ));

结束

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post