• Resolved fabianpnke

    (@fabianpnke)


    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)
  • 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

    (@fabianpnke)

    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.