0

i make two list theme

  1. the posts have featured image and title, but empty content. i want get this list.

  2. and other posts with content list.

I don't have any idea, help me please

    $args = array(
    'post_type' => 'post',
    'post_status' => array('publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash')    
    'posts_per_page' => -1,
    'orderby' => 'ID',
    'order' => 'desc');

$blank_posts = array();

$posts = new WP_Query( $args );

if ( $posts->have_posts() ) : 
    while ( $posts->have_posts() ) : $post = $posts->the_post();
       $content = get_the_content();
        if(empty($content)) { 
            array_push( $blank_posts, $post);
        }
    endwhile;
endif;

/* print all blank content posts */
print_r($blank_posts);

this code doesn't work, Please give me some detailed code, thanks

0

your code :

$content = get_the_content();
        if(empty($content)) {...}

you can replace your code with this :

if ($posts->post_content == ''){...}

this is not tested just try it i hope useful it.

EDIT

try this code.

$args = array(
    'post_type' => 'post',
    'post_status' => array('publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash'),
    'posts_per_page' => -1,
    'orderby' => 'ID',
    'order' => 'desc');

$blank_posts = array();
$content_post =array();

$posts = new WP_Query( $args );

if ( $posts->have_posts() ) : 
    while ( $posts->have_posts() ) : $posts->the_post();
    global $post;
       $content = get_the_content();
        if(empty($content)) { 
            array_push( $blank_posts, $post);
        }else{
            array_push( $content_post, $post);
        }
    endwhile;
endif;
/* print all blank content posts */
var_dump($blank_posts);
var_dump($content_post);

if(!empty($blank_posts)){
    foreach ($blank_posts as $pst) {
        echo "ID= ". $pst->ID . ' : '. "Title= ". $pst->post_title .'<hr />';
    }
}
  • Array ( [0] => [1] => [2] => [3] => [4] => [5] => [6] => [7] => [8] => [9] => [10] => [11] => [12] => ) dont work – metalbug Aug 2 '17 at 5:57
  • @metalbug Please check edited answer. – Jignesh Patel Aug 2 '17 at 6:16
  • array(3) { [0]=> object(WP_Post)#3100 (24) { ["ID"]=> int(51) ["post_author"]=> string(1) "1" ["post_date"]=> string(19) "2017-07-31 13:00:46" ["post_date_gmt"]=> string(19) "0000-00-00 00:00:00" ["post_content"]=> string(0) "" ["post_title"]=> string(12) – metalbug Aug 2 '17 at 6:25
  • look <?php while (have_posts()) : the_post(); ?> this code list all the posts, i want empty content and have content, two list – metalbug Aug 2 '17 at 6:26
  • help me please... – metalbug Aug 2 '17 at 6:26
0

You should get the content field directly, and check it by a custom function.

I wrote a simple one for you:

function check_empty_content($post_id) {
    $str = apply_filters( 'the_content', get_post_field( 'post_content', $post_id ) );
    return trim( str_replace( '&nbsp;', '', strip_tags($str) ) ) == '';
}

Now you can use this function instead of empty().

  • dont work, i need empty content and have content, two list – metalbug Aug 2 '17 at 6:00
  • The code is tested since I pulled it from my project. You should use if (check_empty_content(get_the_ID()) { ... } else { ... } if you want both. – Jack Johansson Aug 2 '17 at 6:06
  • look <?php while (have_posts()) : the_post(); ?> this code list all the posts, i want empty content and have content, two list – metalbug Aug 2 '17 at 6:08

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.