个性化阅读
专注于IT技术分析

wp_query多个元键

点击下载

我想使用”自定义”字段制作一个搜索表单, 可以搜索表postmeta显示报价和信息搜索, 在本地主机中键入代码, 但是该站点无法加载我使用XAMPP是什么问题?

这是我的代码

<?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; ?>

谢谢


#1


你缺少查询的关系部分, 我不确定这不是唯一的问题, 但看起来应该像这样:

$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, 

希望这可以帮助


#2


这可能在游戏中有些晚, 但是查看代码可能未得到任何结果, 因为尚未定义查询之间的关系。默认情况下, WP_Query使用’relation’=>’AND’比较查询的元数据, 这意味着必须在数据库中找到元数据的每一位, 以便查询返回结果, 因此, 如果出于某种原因或其他原因即使在数据库中找不到一个查询数据(或未成功发送到查询), 它也将返回负结果(或在你的情况下不返回任何结果)。

简而言之, 如果要查找非常特定的查询结果(元数据的所有位都应与查询匹配), 则应使用AND运算符;否则, 如果要查找更通用的内容(则应使用OR运算符)将匹配一个元查询或另一个元查询, 或另一个, 依此类推-你就会明白)。我还将确保添加一个否定结果标志, 以便你至少可以首先查看查询是否已发送(和返回)。

考虑到以上几点, 更改后的查询版本将类似于:

    <?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变量是否确实包含有效数据始终是一个好主意, 否则, 你将不得不长时间摸索。

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


#3


<?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 =键名”。

赞(0)
未经允许不得转载:srcmini » wp_query多个元键

评论 抢沙发

评论前必须登录!