0

The following query is returning unexpected results. It is returning results for 24 of the most recently created pages, none of which have a custom field named 'calmonthname' holding the text value 'July.' (calmonthname is a custom select field allowing a single value text choice, of month name).

Can anyone point out any evident problem in the way I've worded the query?

<?php

$args  = array(
    'numberposts' => 1,
    'post_type' => 'page',
    array(
        'key'     => 'calmonthname',
        'value'   => 'July',
        'compare' => '=',
        'type'    => 'CHAR',
    ),
);

$the_query = new WP_Query( $args );

if ( empty( $the_query ) ) {
 exit;
}

if ( $the_query->have_posts()) {
echo
'<ul>';

  while ($the_query->have_posts()) : $the_query->the_post();

    $id = $post->ID; $thumb = get_the_post_thumbnail($id, 'full');
    echo
    '<li>' .
      '<a href="' . the_permalink() . '">' . $thumb . the_title() . (Today) . '</a>' .
    '</li>';

  endwhile;

echo
'</ul>';

}

wp_reset_query();

?>
  • hmmm where did you get that syntax from? That's not how post meta queries work, but keep in mind that post meta queries are super slow, you would get significantly better performance using a taxonomy if you're searching/filtering/querying for posts by that information – Tom J Nowell Jul 6 '18 at 20:14
  • yeah i can see taxonomy would be better for the simplified version I posted. I've reposted what I'm actually after, which I don't think taxonomy will do. Thanks Tom! – Randal Oulton Jul 6 '18 at 23:45
  • I don't see anything in the updated question that changes that, a taxonomy is still superior for performance, and still usable for your use case – Tom J Nowell Jul 7 '18 at 21:40
0

First thing first, mind your formatting. Future you thanks you!

Second, you've just got some weird array sticking around in your $args variable. I believe what you're looking for is meta_query which takes "an array of arrays" to compare by.

Third I believe you're also looking for posts_per_page, not numberposts. posts_per_page is the record limiting argument for WP_Query().

Some other notes, you don't need to concatenate echoed strings on separate lines, just leave the quotation open. I'm not sure what (Today) is supposed to be, a misrepresented string? You also don't need to define $id since you're only using it the one time, just use $post->ID for the thumbnail ID. You also appear to be echoing the_ functions, which are built-in functions that echo the get_ functions (see the_title() vs get_the_title() for more information/)

As Tom mentioned - you may consider a taxonomy for this kind of categorization - however since we don't know the exact scenario for this, I'll leave it as a meta_query:

$args = array(
    'post_type'      => 'page',
    'posts_per_page' => 1,
    'meta_query'     => array(
        array(
            'key'     => 'calmonthname',
            'value'   => 'July',
            'compare' => '=',
            'type'    => 'CHAR',
        )
    ),
);

$the_query = new WP_Query( $args );

if( $the_query->have_posts() ){
    echo '<ul>';
        while( $the_query->have_posts() ) : $the_query->the_post();
            $thumb = get_the_post_thumbnail( $post->ID, 'full');
            echo '<li><a href="'. get_permalink() .'">'. $thumb . get_the_title() .' (Today)</a></li>';
        endwhile;
    echo '</ul>';
}

Update: Based on your code (you can also drop wp_reset_query() and use wp_reset_postdata() instead and our comments, here's an updated answer:

<?php
    $now = new \DateTime('now');
    $day1= $now->format('j');
    $day = strval($day1);
    $day = str_pad($day , 2, '0', STR_PAD_LEFT); //put a 0 in front if single number
    $month1 = $now->format('m');
    $month = strval($month1);
    $month = str_pad($month , 2, '0', STR_PAD_LEFT); //put a 0 in front if single number
    //echo $day . $month;

    $args = array(
        'post_type'    => 'page',
        'meta_query'   => array(
            array(
                'key'     => 'calday',
                'value'   => $day,
                'compare' => '=',
                'type'    => 'CHAR',
            ),
            array(
                'key'     => 'calmonth',
                'value'   => $month,
                'compare' => '=',
                'type'    => 'CHAR',
            ),
            'relation' => 'AND'
        )
    );

    $the_query = new WP_Query( $args );

    if( $the_query->have_posts() ){
        echo '<ul>';
            while( $the_query->have_posts() ) : $the_query->the_post();
                $thumb = get_the_post_thumbnail( $post->ID, 'full');
                echo '<li><a href="'. get_permalink() .'">'. $thumb . get_the_title() .' </a></li>';
            endwhile;
        echo '</ul>';
    }

    wp_reset_postdata();
?>
  • Thank you Xhynk for your help and all the background info, I really appreciate it. And the meta_query is probably a key mistake I was making. – Randal Oulton Jul 6 '18 at 23:44
  • My pleasure. I've flagged the "answer" you wrote, as you should post that as an update to your original question - not an answer. Looking at it briefly, however, your syntax is incorrect. Right now you the values are looking for the literal value '$day', if you want that parsed as a variable you need to remove the quotes: 'value' => $month,. Also the 'relation' => 'AND', needs to go inside the meta_query array, not the $args array. – Xhynk Jul 6 '18 at 23:51
  • thanks gonna try that. i'll see if I can figure out how to post as update too! appreciated! – Randal Oulton Jul 6 '18 at 23:58
  • Houston, we have lift off! Fixing those final dummy errors (can't believe I was putting quotes around a variable, duh!!!) did it. Thank you, thank you, thank you. – Randal Oulton Jul 7 '18 at 0:03
  • Done thank you! And I see my comment incorrectly posted got deleted LMAO. oh well! thanks for help I appreciate it ! :} – Randal Oulton Jul 7 '18 at 0:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.