$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - meta_query not working properly|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp query - meta_query not working properly

matteradmin7PV0评论

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'
        ), 
    ), 
);

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far