loop - Query post with content only

admin2025-06-02  1

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.

Share Improve this question asked Mar 4, 2019 at 12:28 QuentinQuentin 158 bronze badges 4
  • Why wouldn't you just keep the empty posts as draft? Then in your loop use 'publish' for status? – Howard E Commented Mar 4, 2019 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 Commented Mar 4, 2019 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 Commented Mar 5, 2019 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 Commented Mar 6, 2019 at 11:51
Add a comment  | 

1 Answer 1

Reset to default 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";
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748836728a314130.html

最新回复(0)