Title: wp_query for posts ids
Last modified: September 1, 2016

---

# wp_query for posts ids

 *  Resolved [fabianpnke](https://wordpress.org/support/users/fabianpnke/)
 * (@fabianpnke)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/wp_query-for-posts-ids/)
 * Hey there,
 * I’m getting in trouble by creating a post query, with is ending in an $variable
   with post IDs inside. I hope you can help me. 🙂
 * My final result variable should look like this:
    Content of $slogan: “23, 13,
   34, 35”
 * ! This Code should work inside of my header.php !
 * This is the code i have created so far:
 *     ```
       // ID of current page
       global $post;
       $pid = $post->ID;
   
       // ID of Slogan Posts
       // WP_Query arguments
       $args = array (
       	'post_type'         => 'header-slider-type',
       	'cat'                    => '68',
       	'order'                 => 'DESC',
       	'orderby'             => 'date',
       );
   
       // The Query
       $slogan = new WP_Query( $args );
   
       // The Loop
       if ( $slogan->have_posts() ) {
       	while ( $slogan->have_posts() ) {
       		$slogan->the_post();
       		$slogan->get_the_ID();
       	}
       } else {
       		_e( 'Sorry, no posts matched your criteria.' );
       }
   
       // Restore original Post Data
       wp_reset_postdata();
       wp_reset_query();
       ```
   
 * Thank you!!!

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [latro666](https://wordpress.org/support/users/latro666/)
 * (@latro666)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/wp_query-for-posts-ids/#post-7605505)
 * Why not have a different varible
 * e.g.
 *     ```
       $slogan = new WP_Query($args);
       while ($slogan->have_posts()) : $slogan->the_post();
           $myids .= $post->ID . ',';
       }
       $myids = rtrim($myids, ',');
       ```
   
 *  Thread Starter [fabianpnke](https://wordpress.org/support/users/fabianpnke/)
 * (@fabianpnke)
 * [9 years, 10 months ago](https://wordpress.org/support/topic/wp_query-for-posts-ids/#post-7605512)
 * Hey,
 * now I have the correct code:
 *     ```
       $slogan_ids = get_posts(array(
           'numberposts'   => -1, // get all posts.
           'tax_query'     => array(
               array(
       	'post_type'              => 'header-slider-type',
       	'cat'                    => '68',
       	'order'                  => 'DESC',
       	'orderby'                => 'date',
               ),
           ),
           'fields'        => 'ids', // Only get post IDs
       ));
       ```
   
 * It works!!

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘wp_query for posts ids’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 2 replies
 * 2 participants
 * Last reply from: [fabianpnke](https://wordpress.org/support/users/fabianpnke/)
 * Last activity: [9 years, 10 months ago](https://wordpress.org/support/topic/wp_query-for-posts-ids/#post-7605512)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
