Tell us what you are trying to do?
Is it possible to set in view or wordpress archive view a filter that filters out posts from loop that do not have featured image?
Hello,
It is possible with custom codes, you can try the views filter hook wpv_filter_query, for example, for a normal post view( ID 123), you can add below codes into your theme file functions.php:
add_filter( 'wpv_filter_query', 'add_filter_by_thumbnail', 10, 3 );
function add_filter_by_thumbnail ( $query, $view_settings, $view_id ) {
if( $view_id == 123) {
$args = array(
'relation' => 'AND',
array(
'key' => '_thumbnail_id',
'compare' => 'NOT EXISTS'
)
);
// add these arguments to your meta query
$query['meta_query'] = isset($query['meta_query']) ? $query['meta_query'] : [];
$query['meta_query'][] = $args;
}
return $query;
}
Please replace 123 with your view's ID
More help:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
Hi, thanks for the code!
I've added these lines in functions.php and replaced ID with my wordpress archive view id.
Still, it's displaying posts without featured image. Here is a link: hidden link
Hmm, still nothing has changed.
I'm sending functions.php SC in attachment
I assume we are talking about the "apartment" term's archive page
For the WordPress archive, you will need to try WordPress built-in filter hook "pre_get_posts", for example, you can change the PHP codes as below:
add_action( 'pre_get_posts', 'add_filter_by_thumbnail', 99);
function add_filter_by_thumbnail ( $query ) {
if( ! is_admin() && $query->is_main_query() && is_tax('property-type', 'apartment') ) {
$args = array(
'relation' => 'AND',
array(
'key' => '_thumbnail_id',
'compare' => 'EXISTS'
)
);
$meta_query = (array)$query->get('meta_query');
$meta_query[] = $args;
$query->set( 'meta_query', $meta_query );
}
return $query;
}
More help:
https://developer.wordpress.org/reference/hooks/pre_get_posts/
Nice! It works 😀
Is it possible to apply this code to all "property-type" archives? or I need to add this code for each property type category?
For istance, other than apartment I have hotels, rooms, camping, etc. And also translation of them.
You can change this code from:
is_tax('property-type', 'apartment')
To:
is_tax('property-type')
More help:
https://codex.wordpress.org/Function_Reference/is_tax
My issue is resolved now. Thank you!