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();
?>