I need to loop through the last 3 posts movie
, but only those which have a content not empty.
I used to do as follow when I used the ACF editor instead of the native one.
movie-content
refers to the the slug of my WYSIWYG field.
$args = array(
'posts_per_page' => 3,
'post_type' => 'movie',
'meta_query' => array(
array(
'key' => 'movie-content', // ACF editor field name
'value' => '',
'compare' => '!='
)
)
);
Is there any way to do that with the native editor? I'd like to make the conditional test in the request rather than in the loop.
I need to loop through the last 3 posts movie
, but only those which have a content not empty.
I used to do as follow when I used the ACF editor instead of the native one.
movie-content
refers to the the slug of my WYSIWYG field.
$args = array(
'posts_per_page' => 3,
'post_type' => 'movie',
'meta_query' => array(
array(
'key' => 'movie-content', // ACF editor field name
'value' => '',
'compare' => '!='
)
)
);
Is there any way to do that with the native editor? I'd like to make the conditional test in the request rather than in the loop.
If I understand your question fully, I believe this is what you're after.
As far as I know... There is no WP_Query that you can use to accomplish this. However, you can use the $wpdb and make a custom query.
global $wpdb;
$q = " SELECT * from {$wpdb->posts} p
RIGHT JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id AND pm.meta_key = 'your_meta_key'
WHERE p.post_type = 'your_post_type' AND p.post_status = 'publish' and p.post_content <> ' '
LIMIT 3";
$results = $wpdb->get_results($q);
foreach ($results as $post){
// Your Loop goes here
echo $post->post_content;
}
Just replace the 'your_post_type' etc with your custom fields.
Instead of p.post_content, you could also use pm.meta_value <> ' ' - this will retrieve only posts where the value of the meta field you've chosen is not empty.
in which case, the Query would be:
$q = " SELECT * from {$wpdb->posts } p
RIGHT JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id AND pm.meta_key = 'your_meta_key'
WHERE p.post_type = 'your_post_type' AND p.post_status = 'publish' and pm.meta_value <> ' '
LIMIT 3";
post_content
, the content is in the ACF fieldmovie-content
- so you're asking to have a loop where you want the last 3 posts where the meta keymovie-content
is not empty? – Howard E Commented Mar 5, 2019 at 11:34