I only want to show my post with thumbnails. Given below is the code I used to show all the post, but now I need to ammend it in such a way that only post with thumbnails should appear. Also these post are showing on oother page as well where I just need to show them normally, its the homepage where I need to show post with thumbnails. I need quick help. Thanks
$events = new WP_Query($args);
if ($events->have_posts()):
$result .= "<div class='featuredin container'>";
$result .= "<ul class='featured-in-content'>";
while($events->have_posts()): $events->the_post();
$start2 = get_post_meta(get_the_ID(), 'links-ev-sdate', true);
$result .= '<li class="event_url"><a target="_blank" href="'.$start2.'">'. get_the_post_thumbnail();
$result .= '</a></li>';
endwhile;
$result .= '</ul>';
$result .= "</div>";
else:
$result .= '<span>Events Not Found</span>';
endif;
return $result;
I only want to show my post with thumbnails. Given below is the code I used to show all the post, but now I need to ammend it in such a way that only post with thumbnails should appear. Also these post are showing on oother page as well where I just need to show them normally, its the homepage where I need to show post with thumbnails. I need quick help. Thanks
$events = new WP_Query($args);
if ($events->have_posts()):
$result .= "<div class='featuredin container'>";
$result .= "<ul class='featured-in-content'>";
while($events->have_posts()): $events->the_post();
$start2 = get_post_meta(get_the_ID(), 'links-ev-sdate', true);
$result .= '<li class="event_url"><a target="_blank" href="'.$start2.'">'. get_the_post_thumbnail();
$result .= '</a></li>';
endwhile;
$result .= '</ul>';
$result .= "</div>";
else:
$result .= '<span>Events Not Found</span>';
endif;
return $result;
Share
Improve this question
edited Feb 12, 2020 at 14:05
Jacob Peattie
44.2k10 gold badges50 silver badges64 bronze badges
asked Feb 12, 2020 at 13:39
YusraYusra
1
1
|
1 Answer
Reset to default 3You need to define your arguments before passing to WP_Query
. Also, you may use
meta_query
to acheive what you are looking for.
Here is some documentation to help you with your futur developments.
WP_Query
Link to the documentation Show posts associated with a certain custom field.
This part of the query is parsed by WP_Meta_Query
, so check the docs for it as well in case this list of arguments isn’t up to date.
WP_Meta_Query
Link to the documentation Used for generating SQL clauses that filter a primary query according to metadata keys and values. Filter their results by object metadata, by generating JOIN and WHERE subclauses to be attached to the primary SQL query string.
So the goal is to find the metadata key you want to find. To do so, you can go in your database and look for your meta_key
in the table named wp_postmeta
.
In this case, we would search for _thumbnail_id
in our table.
SELECT * FROM wp_postmeta WHERE meta_key LIKE '_thumbnail_id';
Once you found the meta_key
you want to work with, just build your array.
Here is the final code
<?php
// WP_Query arguments
$args = array(
// Your default arguments
'posts_per_page' => 10,
'orderby' => 'post_date',
'order' => 'DESC'
// ...
// This is the part where you need to work with WP_Meta_Query
'meta_query' => array(
'key' => '_thumbnail_id'
)
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// Display your post information
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
?>
$args
is defined in your code? – Jacob Peattie Commented Feb 12, 2020 at 14:05