0

Trying to figure out why this won't work for me. I'm simply trying to run a loop of all posts and pages, and tag them as post and page in a array. But the is_single() conditional is not working, all posts show up as pages. And if I remove the is_page() it'll just give me null

$data = array();
$args = array(
    'posts_per_page' => '-1',
    'post_type' => array('post', 'page'),
    'orderby' => 'menu_order',
    'order' => 'ASC',
);

$loop = new WP_Query($args);
if( $loop->have_posts() ):
    while( $loop->have_posts() ): $loop->the_post();
        $id = $loop->post->ID;        

        if(is_single()) {
            $type = 'article';
        } elseif(is_page()) {
            $type = 'page';
        }

        $data[$id] = array(
            'uri' =>  $loop->post->post_name,
            'title' => apply_filters( 'the_title', $loop->post->post_title ),
            'type' => $type,
            'entry' => apply_filters( 'the_content', $loop->post->post_content ),
        );
    endwhile;
endif;
wp_reset_postdata();
2

is_single() and is_page() refer to the main query and as far as I know are not altered to match the individual posts in a Loop. They refer to the type of page not to the individual posts. Try and you should see what I mean:

$data = array();
$args = array(
    'posts_per_page' => '-1',
    'post_type' => array('post', 'page'),
    'orderby' => 'menu_order',
    'order' => 'ASC',
);

$loop = new WP_Query($args);
if( $loop->have_posts() ):
    while( $loop->have_posts() ): $loop->the_post();
        $id = $post->ID;        
        the_title(); var_dump($post->post_type,is_single(),is_page(),$loop->is_single(),$loop->is_page());
        echo '<br>';
    endwhile;
endif;
wp_reset_postdata();

The simple solution is just to check the post type itself

if('post' == $post->post_type) {
    $type = 'article';
} elseif('page' == $post->post_type) {
    $type = 'page';
}

Also this,

$id = $loop->post->ID;   

is overly complicated. $post->ID is the same value. Or use get_theID()

|improve this answer|||||
  • Thanks, but if I use $post->ID rather than $loop->post->ID it gives me nothing.. – INT Dec 1 '13 at 20:26
  • Also I need to get the categories and tags of these posts. Is that possible even? – INT Dec 1 '13 at 20:27
  • Yes, it should be possible but you will need to explain more, and that addition might qualify as a new question. – s_ha_dum Dec 1 '13 at 20:28
1

is_single() and is_page() operate on the global $wp_query, and won't work in this context. If you're trying to determine post type, check the value of $post->post_type

if( 'post' == $post->post_type ) {
    $type = 'article';
} elseif( 'page' == $post->post_type ) {
    $type = 'page';
}
|improve this answer|||||
  • I appreciate your answer @Milo but since you answered at the same time as s_ha_dum and his answer is more elaborate I have to go with that one. Thanks alot though! – INT Dec 1 '13 at 20:43
0

You could force is_single when replacing the main query:

$draft_post_query = new WP_query(array(
    'post_status' => 'draft',
    'post_type' => array('post'),
    'post__in' => array($post_id)
));
$draft_post = $draft_post_query->post;

global $wp_query, $post;
$wp_query = $draft_post_query;
$post = $draft_post;
$wp_query->is_single = true;
$wp_query->is_singular = true;
|improve this answer|||||

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.