WordPress meta_query无法按预期工作

时间:2018-10-21 01:08:37

标签: php wordpress meta-key

如果当前页面的网址在meta_key popup_display_pages中,我试图插入弹出窗口。 popup_display_pages是一个数组。

未显示添加了meta_query的优惠券。我是否使用了不正确的meta_query?

        <?php global $wp;
            $currentURL = home_url( $wp->request );

            $args = array (
                'post_type' => 'popup',
                'posts_per_page' => '-1',
                'meta_query' => array(
                    array(
                        'key' => 'popup_display_pages',
                        'value' => $currentURL,
                        'compare' => 'IN'
                    )
                )
            );

            $recent = new WP_Query($args); if($recent->have_posts()) :?>
                <?php while($recent->have_posts()) : $recent->the_post();?>
                    <div class="coupon test">
                        <?php the_content();?>
                    </div>
                <?php endwhile;
            endif;?>

编辑1

$ currentURL的Var_Dump:字符串(31)“ http://localhost:8888/test-page” $ recent的Var_Dump:https://hastebin.com/lihojohabi.cpp

问题的答案:

  1. 是的帖子类型称为弹出窗口
  2. 这是存储在meta_key popup_display_pages中的数组:

a:2:{i:0; s:31:“ http://localhost:8888/test-page”; i:1; a:1:{i:0; s:31:“ http://localhost:8888/test-page”;} }

1 个答案:

答案 0 :(得分:0)

在查询中,您正在查询包含www.domainname.example / your-current-path的帖子类型“ popup”的帖子。

但是,如果您的元值存储为http://www.domainname.example/your-current-path/,则可能没有匹配项。 (请注意末尾的斜杠和/或http://)。

因此,var_dump $ currentUrl会检查该值并知道查询中正在使用什么,然后检查该值是否符合预期。有关使用home_url构建currentPath的更多信息,请参见:https://codex.wordpress.org/Function_Reference/home_url

相关问题