$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>loop - Query post with content only|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

loop - Query post with content only

matteradmin7PV0评论

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

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far