Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to look up a page id and determine if it's a page or post. Then query that id and display stuff like title, featured image, etc.

This works for Pages:

    query_posts('page=' . $featured_tab); 

This works or Posts:

    query_posts('p=' . $featured_tab); 

This will tell me if $featured_tag is a post or page:

    get_post_type( $featured_tab ) 

But this does not work

    foreach ( $featarr as $featured_tab ) {
        if (get_post_type( $featured_tab ) == 'page') {
            $featured_tab_type = 'page=';
        } elseif (get_post_type( $featured_tab ) == 'post') {
            $featured_tab_type = 'p=';
        }
        query_posts($featured_tab_type . $featured_tab); 

        while (have_posts()) : the_post();    

           Echo 'stuff here';

The problem I am having is this...the $featured_tag variable represents an assigned page/post id that is displayed on a page.

So I feed in a string of id's, i.e., 34,543,2432,355... etc then take each id as $featured_tab and query for title, featured page and excerpt.

Then I display all the page/post id's in a three column table.

This work perfect but only if I query for one or the other but not both as I do in the if statement if post use p= or if page use page=.

When I use the if condition is get crazy results here if there are 9 ids, I get 50+ results...duplicates and only post no page are displayed.

share|improve this question
    
Is it throwing any errors or is it just not displaying? –  imHavoc Dec 2 '12 at 23:53
    
I updated the post, at the bottom. –  Jason Dec 3 '12 at 0:30
1  
Can you explain a bit better what your trying to actually do, this looks very weird. –  Wyck Dec 3 '12 at 1:59
    
Inside the theme admin I assign a string of page id's to be displayed on the home page. You can see an example at hearfitclinic.com/members. Inside my template page I loop through each id and display the the page id's title, featured image and excerpt. Using query_posts works just fine if I only want the retrieve Pages query_post(page=34) for just pages or query_posts(p=3435` for just posts but I need to query both. –  Jason Dec 3 '12 at 6:35
    
query_posts('p='. $featured_tab); query_posts('page='. $featured_tab); work just fine. So I tried to add a condition that checks if the id is a post or page get_post_type() which outputs 'post' or 'page' and based on that use either 'p=' or 'page='...query_posts($featured_tab_type . $featured_tab) –  Jason Dec 3 '12 at 6:40
add comment

2 Answers 2

You don't have to determine what type it is to query it, you just have to set the proper arguments to override defaults. First off, we'll use WP_Query to do additional queries instead of query_posts.

If we only set the p argument, post_type defaults to post, so we won't get any pages:

$query = new WP_Query(
    array(
        'p' => 13
    )
);

If we just add post_type and set it to any, we'll get whatever post ID 13 is, whether it's a post, page, attachment, etc..

$query = new WP_Query(
    array(
        'post_type' => 'any',
        'p' => 13
    )
);

If you have some post IDs and need to query all of them, you can get rid of the foreach loop and just do a single query for all the posts at once with the post__in argument:

$query = new WP_Query(
    array(
        'post_type' => 'any',
        'post__in' => array( 1, 2, 13, 42 )
    )
);
share|improve this answer
    
I have a problem with new WP_query...Thanks, I did see new WP_Query in the codex but was not sure how to pass the id...I have a couple questions: WP_Query runs as a function but new WP_Query does not (at least not as I have it). How do I run new WP_Query with out assigning it to a variable? Or what do I do with $query, tried to echo it and print it? ..... in either case the page breaks. Second question: there shouldn't be a problem if I assign the id as 'p' => $featured_tab right? However, I did pass a valid id number without using #featured_tab and the page still breaks. –  Jason Dec 3 '12 at 6:28
    
read interacting with WP_Query to see how to use it with the loop. query_posts itself calls WP_Query, but it modifies global variables that your theme may elsewhere rely on, so it should not be used beyond the main query. the key bit of info for your case is understanding the defaults when you don't explicitly set them- post_type will default to post if one is not specified, which is why you can't query a page using only p=. for your needs though, I think using post__in would be most efficient. –  Milo Dec 3 '12 at 18:00
add comment

Ok figured it out:

query_posts(array (     
   'p' => $featured_tab,
   'post_type' => array('post', 'page'),
    )       
);

whereas $featured_tab is the id you are querying.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.