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'
),
),
);
$args
has 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_posts
to later the main query. Also, ameta_query
should be an array of an array. Also, do you really want theLIKE
operator.LIKE
comparisons are quite slow – Pieter Goosen Commented Apr 12, 2016 at 4:24