首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >wp_query多个元键

wp_query多个元键
EN

Stack Overflow用户
提问于 2017-01-23 23:22:09
回答 3查看 4.2K关注 0票数 0

我想做一个搜索表,使用自定义字段可以搜索表postmeta Show ticks和信息搜索,在本地主机上输入代码,但网站不加载我使用的XAMPP有什么问题?

这是我的代码

代码语言:javascript
复制
<?php
$args = array(
    'post_type' => 'house',
    'meta_query' => array(
        array(
            'key' => 'city',
            'value' => $_GET["city"],
            'compare' => 'LIKE',
        ),
        array(
            'key' => 'part',
            'value' => $_GET["part"],
            'compare' => 'LIKE',
        ),
        array(
            'key' => 'statuss',
            'value' => $_GET["statuss"],
            'compare' => 'LIKE',
        ),
        array(
            'key' => 'typee',
            'value' => $_GET["typee"],
            'compare' => 'LIKE',
        ),
        array(
            'key' => 'statuss',
            'value' => $_GET["statuss"],
            'compare' => 'LIKE',
        ),
        array(
            'key' => 'rooms',
            'value' => $_GET["rooms"],
            'compare' => 'LIKE',
        ),
        array(
            'key' => 'wcss',
            'value' => $_GET["wcss"],
            'compare' => 'LIKE',
        ),
        array(
            'key' => 'metr',
            'value' => $_GET["metr"],
            'compare' => 'LIKE',
        ),
        array(
            'key' => 'minip',
            'value' => $_GET["minip"],
            'compare' => 'LIKE',
        ),
        array(
            'key' => 'maxp',
            'value' => $_GET["maxp"],
            'compare' => 'LIKE',
        ),
        array(
            'key' => 'tabaghe',
            'value' => $_GET["tabaghe"],
            'compare' => 'LIKE',
        ),
        array(
            'key' => 'rahn',
            'value' => $_GET["rahn"],
            'compare' => 'LIKE',
        ),
        array(
            'key' => 'ejareh',
            'value' => $_GET["ejareh"],
            'compare' => 'LIKE',
        )
    )
);
$all_posts = new WP_Query($args);
if ($all_posts->have_posts()) :?>
    <div class="titSearchHouse"
         style="text-align: center;width: 150px;margin: 12px auto 0;background: #d0d0d0;color: #FFFFFF;font-family: 'B Yekan';padding: 10px">
        display results
    </div>
    <div class="parti">
        <?php while ($all_posts->have_posts()):$all_posts->the_post(); ?>
            <!--start post wrapper-->
            <a class="post-link" href="<?php echo get_the_permalink(); ?>">
                <div class="post wow fadeInUp">
                    <div class="post-inner">
                        <div class="post-thumb">
                            <?php echo get_the_post_thumbnail($all_posts->post->ID, 'main-thumbnail'); ?>
                        </div>
                        <span
                            class="post-title"><?php echo get_the_title($all_posts->post->ID); ?></span>
                    </div>
                    <div class="post-meta">
                                                            <span><i
                                                                    class="fa fa-clock-o"></i><?php echo get_the_date('Y-m-d', $all_posts->post->ID); ?></span>
                        <span><i class="fa fa-user"></i><?php echo get_the_author(); ?></span>
                        <span><i class="fa fa-thumbs-o-up"></i>506</span>
                    </div>
                </div>
            </a>
            <!--end post wrapper-->
        <?php endwhile; ?>
    </div>
    wp_reset_postdata();
<?php endif; ?>

谢谢你

EN

回答 3

Stack Overflow用户

发布于 2017-01-23 23:45:44

您缺少查询中的关系部分,我不确定这是否是唯一的问题,但它应该如下所示:

代码语言:javascript
复制
$args = array(
    'post_type' => 'house',
    'meta_query' => array(
        'relation' => 'AND',
        array(
            'key' => 'city',
            'value' => $_GET["city"],
            'compare' => 'LIKE',
        ),
        array(
            'key' => 'part',
            'value' => $_GET["part"],
            'compare' => 'LIKE',
        ),
        array..... etc,

希望这能有所帮助

票数 1
EN

Stack Overflow用户

发布于 2018-08-20 20:19:09

这在游戏中可能有点晚了,但是查看您的代码可能得不到任何结果,因为您没有定义查询之间的关系。默认情况下,WP_Query使用'relation‘=> 'AND’来比较查询的元数据,这意味着为了让查询返回结果,必须找到数据库中存在的每一位元数据,所以如果由于某种原因或其他原因,甚至有一段查询数据在数据库中找不到(或者没有成功发送到查询),它将返回负结果(或者在您的情况下不返回任何结果)。

简而言之,如果你正在寻找非常具体的查询结果(所有元数据都应该与你的查询匹配),那么你应该使用AND运算符,否则,如果你寻找更一般的东西(它将匹配一个元查询或另一个或另一个,依此类推,你就明白了)。我还会确保包含一个否定结果标志,这样您至少可以在第一时间看到您的查询是否被发送(和返回)。

记住以上内容,查询的更改版本将如下所示:

代码语言:javascript
复制
    <?php
        $args = array(
        'post_type' => 'house',
        'meta_query' => array(
            'relation' => 'OR',
            array(
                'key' => 'city',
                'value' => $_GET["city"],
                'compare' => 'LIKE',
            ),
            array(
                'key' => 'part',
                'value' => $_GET["part"],
                'compare' => 'LIKE',
            ),
            array(
                'key' => 'statuss',
                'value' => $_GET["statuss"],
                'compare' => 'LIKE',
            ),
            array(
                'key' => 'typee',
                'value' => $_GET["typee"],
                'compare' => 'LIKE',
            ),
            array(
                'key' => 'statuss',
                'value' => $_GET["statuss"],
                'compare' => 'LIKE',
            ),
            array(
                'key' => 'rooms',
                'value' => $_GET["rooms"],
                'compare' => 'LIKE',
            ),
            array(
                'key' => 'wcss',
                'value' => $_GET["wcss"],
                'compare' => 'LIKE',
            ),
            array(
                'key' => 'metr',
                'value' => $_GET["metr"],
                'compare' => 'LIKE',
            ),
            array(
                'key' => 'minip',
                'value' => $_GET["minip"],
                'compare' => 'LIKE',
            ),
            array(
                'key' => 'maxp',
                'value' => $_GET["maxp"],
                'compare' => 'LIKE',
            ),
            array(
                'key' => 'tabaghe',
                'value' => $_GET["tabaghe"],
                'compare' => 'LIKE',
            ),
            array(
                'key' => 'rahn',
                'value' => $_GET["rahn"],
                'compare' => 'LIKE',
            ),
            array(
                'key' => 'ejareh',
                'value' => $_GET["ejareh"],
                'compare' => 'LIKE',
            )
        ));
        $all_posts = new WP_Query($args);
        if ($all_posts->have_posts()): ?>
        <div class="titSearchHouse" style="text-align: center;width: 150px;margin: 12px auto; 
        background: #d0d0d0;color: #FFFFFF;font-family: 'B Yekan';padding: 10px">
            display results
        </div>
        <div class="parti">
            <?php while ($all_posts->have_posts()):$all_posts->the_post(); ?>
                <!--start post wrapper-->
                <a class="post-link" href="<?php echo get_the_permalink(); ?>">
                    <div class="post wow fadeInUp">
                        <div class="post-inner">
                            <div class="post-thumb">
                                <?php echo get_the_post_thumbnail($all_posts->post->ID, 
        'main-thumbnail'); ?>
                            </div>
                            <span
                                class="post-title"><?php echo get_the_title($all_posts->post- 
        >ID); ?></span>
                        </div>
                        <div class="post-meta">
                                                                <span><i
                                                                        class="fa fa-clock- 
        o"></i><?php echo get_the_date('Y-m-d', $all_posts->post->ID); ?></span>
                            <span><i class="fa fa-user"></i><?php echo get_the_author(); ?> 
       </span>
                            <span><i class="fa fa-thumbs-o-up"></i>506</span>
                        </div>
                    </div>
                </a>
                <!--end post wrapper-->
            <?php endwhile; ?>
        </div>
        <?php wp_reset_postdata(); ?>
        <?php 
<!--add some error text to your query so that you at least know whether or not the query is working-->
else: ?>

        Sorry, no results match your query parameters.

        <?php endif; ?>

对于初始查询,最好首先使用OR运算符,特别是当您有一个像这样的复杂查询时。这样,您实际上可以看到是否至少返回了部分结果,并排除了任何潜在的错误。此外,处理$_GET或$_POST数据时,最好检查$_GET/$_POST变量是否包含有效数据,否则很长一段时间都会碰壁。

希望这对某些人有帮助!干杯。

票数 1
EN

Stack Overflow用户

发布于 2019-07-30 23:08:09

代码语言:javascript
复制
<?php
        $args = array(
        'post_type' => 'house',
        'meta_key' => {keyname}, <-!!!!!!!!!!!!!!!!!!
        'meta_query' => array(
            'relation' => 'OR',
            array(
                'key' => 'city',
                'value' => $_GET["city"],
                'compare' => 'LIKE',
            ),...
?>

您忘记在查询中放置meta_key!

根据https://developer.wordpress.org/reference/classes/wp_query/的说法

‘meta_value’-请注意,查询中还必须存在‘meta_key=keyname’。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41809734

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档