How to fetch all rows with where condition in wordpress?

Am newbie in WordPress refer my below coding, it’s not getting all records where:

post_author ='1' and post_type ='attachment'

It’s just return single row only. In my db have 20 rows where:

Read More
post_author ='1' and post_type ='attachment'

This is my php code:

<?php
    $args = array('author' => '1' ,'post_type' => 'attachment');
    $query = new WP_Query($args);
    if ($query->have_posts()) {
        while ($query->have_posts()) {
            $query->the_post();

            // now $query->post is WP_Post Object, use:
            echo $query->post->ID;
        }
    }
?>

Related posts

Leave a Reply

1 comment

  1. Try below code

    Use a custom query

        $user_id = 1;
    $the_query = new WP_Query( array( 'post_type' => 'attachment', 'post_status' => 'inherit', 'author' => $user_id) );
    if ( $the_query->have_posts() ) while ( $the_query->have_posts() ) : $the_query->the_post();
       the_title();
    endwhile;