Php wp_查询多个元键
我想使用 自定义字段 我可以搜索表格 postmeta显示标记和信息搜索,在本地主机中键入代码,但站点不加载 我使用XAMPP 有什么问题 这是我的密码Php wp_查询多个元键,php,wordpress,wordpress-theming,Php,Wordpress,Wordpress Theming,我想使用 自定义字段 我可以搜索表格 postmeta显示标记和信息搜索,在本地主机中键入代码,但站点不加载 我使用XAMPP 有什么问题 这是我的密码 <?php $args = array( 'post_type' => 'house', 'meta_query' => array( array( 'key' => 'city', 'value' => $_GET["city"],
<?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; ?>
显示结果
wp_reset_postdata();
谢谢您缺少查询的关系部分,我不确定这是否是唯一的问题,但它应该是这样的:
$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,
希望这对您有所帮助您缺少查询的关系部分,我不确定这是否是唯一的问题,但它应该是这样的:
$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,
希望这有帮助,这可能在游戏中有点晚了,但是查看您的代码,您可能没有得到任何结果,因为您没有定义查询之间的关系。默认情况下,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运算符,特别是如果您有一个复杂的查询,比如这个查询。通过这种方式,您实际上可以看到是否至少返回了一些结果,并排除了任何潜在的错误。另外,在使用$\u GET或$\u POST数据时,最好检查$\u GET/$\u POST变量是否确实包含有效数据,否则将导致长时间的头痛
希望这对别人有帮助!干杯。这可能在游戏中有点晚了,但是看看你的代码,你可能没有得到任何结果,因为你没有定义查询之间的关系。默认情况下,WP_Query使用“relation”=>“AND”来比较查询的元数据,这意味着必须找到数据库中存在的每一位元数据,以便查询返回结果,因此,如果出于某种原因或其他原因,甚至在数据库中找不到一条查询数据(或未成功发送到查询)它将返回负面结果(或者在您的情况下不返回任何结果)
<?php
$args = array(
'post_type' => 'house',
'meta_key' => {keyname}, <-!!!!!!!!!!!!!!!!!!
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'city',
'value' => $_GET["city"],
'compare' => 'LIKE',
),...
?>
简而言之,如果您要查找非常特定的查询结果(所有元数据位都应与您的查询相匹配),则应使用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运算符,特别是如果您有一个复杂的查询,比如这个查询。通过这种方式,您实际上可以看到是否至少返回了一些结果,并排除了任何潜在的错误。另外,在使用$\u GET或$\u POST数据时,最好检查$\u GET/$\u POST变量是否确实包含有效数据,否则将导致长时间的头痛
希望这对别人有帮助!干杯。
我知道,但还没有能力评论,只是觉得我应该指出:)我知道,但还没有能力评论,只是觉得我应该指出:)
<?php
$args = array(
'post_type' => 'house',
'meta_key' => {keyname}, <-!!!!!!!!!!!!!!!!!!
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'city',
'value' => $_GET["city"],
'compare' => 'LIKE',
),...
?>