wp query - meta_query not working properly

admin2025-06-06  6

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 $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: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_posts to later the main query. Also, a meta_query should be an array of an array. Also, do you really want the LIKE operator. LIKE comparisons 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
 |  Show 1 more comment

1 Answer 1

Reset to default 2

Based 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'
        ), 
    ), 
);
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749182806a317053.html

最新回复(0)