admin管理员组文章数量:1130349
I'm trying to run a query that only displays items that meet a condition in an Advanced Custom Fields select box, but I'm getting nothing. Here's my query. Any help would be appreciated:
<?php $args = array(
'post_type' => 'home_plans',
'orderby'=> 'date',
'order' => 'rand',
'numberposts' => '12',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'display_where',
'value' => 'here',
'compare' => 'LIKE'
)
)
); ?>
<div id="ms-container" class="row archive">
<ul id="posts_list">
<?php $recent_posts = wp_get_recent_posts( $args );
$selected = get_field('display_where');
foreach( $recent_posts as $recent ){
get_template_part( 'template-parts/plan-archive-loop', get_post_format() );
}
//wp_reset_postdata();
?>
</ul>
</div>
{edit}Code has changed a bit. Here's the new code:
<?php $archive_args = array(
'post_type' => 'speight_home_plans',
'orderby'=> 'title',
'order' => 'ASC',
'posts_per_page' => 12,
'paged' => $paged,
'page' => $paged,
'meta_query' => array(
'key' => 'display_where',
'value' => 'speight',
'compare' => 'LIKE'
)
);
$archive_query = new WP_Query( $archive_args );
if ( $archive_query->have_posts() ) :
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$total_posts = $archive_query->found_posts;
$start_post = ($paged - 1) * $posts_per_page + 1;
$end_post = min($start_post + $posts_per_page - 1, $total_posts);
echo "<p class=results-count>Showing $start_post - $end_post of $total_posts home plans.</p>";
while ( $archive_query->have_posts() ) : $archive_query->the_post();
get_template_part( 'template-parts/plan-archive-loop', get_post_format() );
endwhile;
wp_reset_postdata();
endif;
This is in the archive-speight_home_plans.php file of my theme.
I'm trying to run a query that only displays items that meet a condition in an Advanced Custom Fields select box, but I'm getting nothing. Here's my query. Any help would be appreciated:
<?php $args = array(
'post_type' => 'home_plans',
'orderby'=> 'date',
'order' => 'rand',
'numberposts' => '12',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'display_where',
'value' => 'here',
'compare' => 'LIKE'
)
)
); ?>
<div id="ms-container" class="row archive">
<ul id="posts_list">
<?php $recent_posts = wp_get_recent_posts( $args );
$selected = get_field('display_where');
foreach( $recent_posts as $recent ){
get_template_part( 'template-parts/plan-archive-loop', get_post_format() );
}
//wp_reset_postdata();
?>
</ul>
</div>
{edit}Code has changed a bit. Here's the new code:
<?php $archive_args = array(
'post_type' => 'speight_home_plans',
'orderby'=> 'title',
'order' => 'ASC',
'posts_per_page' => 12,
'paged' => $paged,
'page' => $paged,
'meta_query' => array(
'key' => 'display_where',
'value' => 'speight',
'compare' => 'LIKE'
)
);
$archive_query = new WP_Query( $archive_args );
if ( $archive_query->have_posts() ) :
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$total_posts = $archive_query->found_posts;
$start_post = ($paged - 1) * $posts_per_page + 1;
$end_post = min($start_post + $posts_per_page - 1, $total_posts);
echo "<p class=results-count>Showing $start_post - $end_post of $total_posts home plans.</p>";
while ( $archive_query->have_posts() ) : $archive_query->the_post();
get_template_part( 'template-parts/plan-archive-loop', get_post_format() );
endwhile;
wp_reset_postdata();
endif;
This is in the archive-speight_home_plans.php file of my theme.
Share Improve this question edited Apr 12, 2016 at 4:00 Laura Sage asked Apr 12, 2016 at 0:35 Laura SageLaura Sage 2255 silver badges11 bronze badges 6 | Show 1 more comment1 Answer
Reset to default 2Based on the codex, the meta_query parameter contains one or more array with the relation parameter not set if single inner meta_query array.
Also remove the page parameter as it serves only for a Static Front Page.
Your args array should look like that:
$archive_args = array(
'post_type' => 'speight_home_plans',
'orderby'=> 'title',
'order' => 'ASC',
'posts_per_page' => 12,
'paged' => $paged,
'meta_query' => array(
array(
'key' => 'display_where',
'value' => 'speight',
'compare' => 'LIKE'
),
),
);
I'm trying to run a query that only displays items that meet a condition in an Advanced Custom Fields select box, but I'm getting nothing. Here's my query. Any help would be appreciated:
<?php $args = array(
'post_type' => 'home_plans',
'orderby'=> 'date',
'order' => 'rand',
'numberposts' => '12',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'display_where',
'value' => 'here',
'compare' => 'LIKE'
)
)
); ?>
<div id="ms-container" class="row archive">
<ul id="posts_list">
<?php $recent_posts = wp_get_recent_posts( $args );
$selected = get_field('display_where');
foreach( $recent_posts as $recent ){
get_template_part( 'template-parts/plan-archive-loop', get_post_format() );
}
//wp_reset_postdata();
?>
</ul>
</div>
{edit}Code has changed a bit. Here's the new code:
<?php $archive_args = array(
'post_type' => 'speight_home_plans',
'orderby'=> 'title',
'order' => 'ASC',
'posts_per_page' => 12,
'paged' => $paged,
'page' => $paged,
'meta_query' => array(
'key' => 'display_where',
'value' => 'speight',
'compare' => 'LIKE'
)
);
$archive_query = new WP_Query( $archive_args );
if ( $archive_query->have_posts() ) :
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$total_posts = $archive_query->found_posts;
$start_post = ($paged - 1) * $posts_per_page + 1;
$end_post = min($start_post + $posts_per_page - 1, $total_posts);
echo "<p class=results-count>Showing $start_post - $end_post of $total_posts home plans.</p>";
while ( $archive_query->have_posts() ) : $archive_query->the_post();
get_template_part( 'template-parts/plan-archive-loop', get_post_format() );
endwhile;
wp_reset_postdata();
endif;
This is in the archive-speight_home_plans.php file of my theme.
I'm trying to run a query that only displays items that meet a condition in an Advanced Custom Fields select box, but I'm getting nothing. Here's my query. Any help would be appreciated:
<?php $args = array(
'post_type' => 'home_plans',
'orderby'=> 'date',
'order' => 'rand',
'numberposts' => '12',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'display_where',
'value' => 'here',
'compare' => 'LIKE'
)
)
); ?>
<div id="ms-container" class="row archive">
<ul id="posts_list">
<?php $recent_posts = wp_get_recent_posts( $args );
$selected = get_field('display_where');
foreach( $recent_posts as $recent ){
get_template_part( 'template-parts/plan-archive-loop', get_post_format() );
}
//wp_reset_postdata();
?>
</ul>
</div>
{edit}Code has changed a bit. Here's the new code:
<?php $archive_args = array(
'post_type' => 'speight_home_plans',
'orderby'=> 'title',
'order' => 'ASC',
'posts_per_page' => 12,
'paged' => $paged,
'page' => $paged,
'meta_query' => array(
'key' => 'display_where',
'value' => 'speight',
'compare' => 'LIKE'
)
);
$archive_query = new WP_Query( $archive_args );
if ( $archive_query->have_posts() ) :
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$total_posts = $archive_query->found_posts;
$start_post = ($paged - 1) * $posts_per_page + 1;
$end_post = min($start_post + $posts_per_page - 1, $total_posts);
echo "<p class=results-count>Showing $start_post - $end_post of $total_posts home plans.</p>";
while ( $archive_query->have_posts() ) : $archive_query->the_post();
get_template_part( 'template-parts/plan-archive-loop', get_post_format() );
endwhile;
wp_reset_postdata();
endif;
This is in the archive-speight_home_plans.php file of my theme.
Share Improve this question edited Apr 12, 2016 at 4:00 Laura Sage asked Apr 12, 2016 at 0:35 Laura SageLaura Sage 2255 silver badges11 bronze badges 6-
It looks like
$argshas potential of being globally available, which may cause problems. We need to see what file, where file this is in context of your system inside WordPress. – Nathan Powell Commented Apr 12, 2016 at 2:48 - Ok. I've changed things to $archive_args and $archive_query to hopefully mitigate this issue. But it's still not reading the meta query array. This is for an archive page for a custom post type. I'll put my new code (been fiddling around with other ideas for getting it to work so it's changed a bit) (ugh...it's not letting me post all of it as a comment...hang on...putting in an "answer" (I guess that's the only way?) – Laura Sage Commented Apr 12, 2016 at 3:53
- Awesome, that should help. Reflect that change in your question by editing it please. Also note what file this is in of your theme or plugin. – Nathan Powell Commented Apr 12, 2016 at 3:56
-
You should not be running a custom query in place of the main query. Use
pre_get_poststo later the main query. Also, ameta_queryshould be an array of an array. Also, do you really want theLIKEoperator.LIKEcomparisons are quite slow – Pieter Goosen Commented Apr 12, 2016 at 4:24 - I don't care what method it uses, as long as I get the right results. What would you suggest as the proper code then? – Laura Sage Commented Apr 12, 2016 at 4:53
1 Answer
Reset to default 2Based on the codex, the meta_query parameter contains one or more array with the relation parameter not set if single inner meta_query array.
Also remove the page parameter as it serves only for a Static Front Page.
Your args array should look like that:
$archive_args = array(
'post_type' => 'speight_home_plans',
'orderby'=> 'title',
'order' => 'ASC',
'posts_per_page' => 12,
'paged' => $paged,
'meta_query' => array(
array(
'key' => 'display_where',
'value' => 'speight',
'compare' => 'LIKE'
),
),
);
本文标签: wp querymetaquery not working properly
版权声明:本文标题:wp query - meta_query not working properly 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749182782a2328888.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


$argshas potential of being globally available, which may cause problems. We need to see what file, where file this is in context of your system inside WordPress. – Nathan Powell Commented Apr 12, 2016 at 2:48pre_get_poststo later the main query. Also, ameta_queryshould be an array of an array. Also, do you really want theLIKEoperator.LIKEcomparisons are quite slow – Pieter Goosen Commented Apr 12, 2016 at 4:24