admin管理员组文章数量:1122846
Im using a custom post type named property
to list some houses and flats. In this cpt, I have multiple custom fields from Advanced Custom Fields plugin, such as localization
, price
, rooms
and so on. This is the HTML form I'm using to filter the results using GET
method
<form>
<button type="submit">filter</button>
<select name="orderby" id="">
<option value="" disabled selected>Order by:</option>
<option value="all">All</option>
<option value="ASC">Lower price</option>
<option value="DESC">Higher price</option>
</select>
<h3>Type of contract</h3>
<select name="contract">
<option value="" selected disabled>Select an option</option>
<option value="rent">Rent</option>
<option value="for_sale">For sale</option>
</select>
...
</form>
In my loop I have this array, based on Codex. I'm using an example of nested arrays, did not work.
<?php
$args = array(
'post_type' => 'property',
'posts_per_page' => -1,
'meta_key' => 'price',
'orderby' => 'meta_value_num',
'order' => $_GET['orderby'],
'meta_query' => array(
'relation' => 'AND',
array(
array(
'key' => 'contract',
'value' => $_GET['contract'],
'compare' => '='
),
array(
'key' => 'rooms',
'value' => $_GET['rooms'],
'compare' => '='
),
...
$query = new WP_Query($args);
if($query->have_posts()) : while($query->have_posts()) : $query->the_post()
...
So for the listing of cpt based on this form as a filter using GET parameters, also ordering price by ASC or DESC, what should I do?
Im using a custom post type named property
to list some houses and flats. In this cpt, I have multiple custom fields from Advanced Custom Fields plugin, such as localization
, price
, rooms
and so on. This is the HTML form I'm using to filter the results using GET
method
<form>
<button type="submit">filter</button>
<select name="orderby" id="">
<option value="" disabled selected>Order by:</option>
<option value="all">All</option>
<option value="ASC">Lower price</option>
<option value="DESC">Higher price</option>
</select>
<h3>Type of contract</h3>
<select name="contract">
<option value="" selected disabled>Select an option</option>
<option value="rent">Rent</option>
<option value="for_sale">For sale</option>
</select>
...
</form>
In my loop I have this array, based on Codex. I'm using an example of nested arrays, did not work.
<?php
$args = array(
'post_type' => 'property',
'posts_per_page' => -1,
'meta_key' => 'price',
'orderby' => 'meta_value_num',
'order' => $_GET['orderby'],
'meta_query' => array(
'relation' => 'AND',
array(
array(
'key' => 'contract',
'value' => $_GET['contract'],
'compare' => '='
),
array(
'key' => 'rooms',
'value' => $_GET['rooms'],
'compare' => '='
),
...
$query = new WP_Query($args);
if($query->have_posts()) : while($query->have_posts()) : $query->the_post()
...
So for the listing of cpt based on this form as a filter using GET parameters, also ordering price by ASC or DESC, what should I do?
Share Improve this question asked Dec 15, 2020 at 16:38 LuanLuan 1831 gold badge1 silver badge8 bronze badges1 Answer
Reset to default 0For meta_query with ACF and ordering price by ASC or DESC try following code.
$args = array(
'post_type' => 'property',
'posts_per_page' => -1,
'meta_key' => 'price',
'orderby' => 'meta_value_num',
'order' => $_GET['orderby'],
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'contract',
'value' => $_GET['contract'],
),
array(
'key' => 'rooms',
'value' => $_GET['rooms'],
),
...
),
);
$query = new WP_Query($args);
if($query->have_posts()) :
while($query->have_posts()) : $query->the_post();
// your code
endwhile;
endif;
本文标签: Filter result of Custom Post Type using metaquery with ACF
版权声明:本文标题:Filter result of Custom Post Type using meta_query with ACF 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736287128a1927813.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论