0

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.

4
  • Why wouldn't you just keep the empty posts as draft? Then in your loop use 'publish' for status? – Howard E Mar 4 '19 at 14:14
  • Because I have a tons of non empty posts which I still need to display the thumb/title/date and other ACF fields in the archive. – Quentin Veron Mar 4 '19 at 17:38
  • So you're not using post_content, the content is in the ACF field movie-content - so you're asking to have a loop where you want the last 3 posts where the meta key movie-content is not empty? – Howard E Mar 5 '19 at 11:34
  • Using ACF was just an example about how I could proceed. I need to display last 3 post types which have a basic content not empty. – Quentin Veron Mar 6 '19 at 11:51
0

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";

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.