• I thought this would be easy…err

    I want to query a specific post (using the post id) which has a different post_type then “post”.

    my post_type = books
    i want to get post = 9

    my query = query_posts(array(‘p’=>9, ‘post_types’=>books));

    This doesn’t work. It ignore the first argument and it just get’s all posts with the post_type book.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Give this a try:

    $my_query = query_posts(array(‘post_id’=>9, ‘post_type’=>’books’));

    Thread Starter gimperdaniel

    (@gimperdaniel)

    Nope, that doesn’t work. The only solution I found right now is to use post__in

    The only problem with that is that when I’m querying several posts it doesn’t keep them in order, it seems to choose an random order. I’ve tried orderby options, but they don’t help much. I need them to be queried in the array order.

    Strange, that worked for me (with a different post_id and post_type, of course).

    <?php
    query_posts('post_id=548&post_type=parks');
    while (have_posts()): the_post();
       the_title();
       the_content();
    endwhile;
    ?>

    This also worked:

    <?php
    $my_query = query_posts('post_id=548&post_type=parks');
    global $post;
    foreach ($my_query as $post) {
       setup_postdata($post);
       the_title();
       the_content();
    }
    ?>

    This worked for sorting in the array order:

    <?php
    $ids = array(548,555,587,583,585);
    $my_query = query_posts(array('post__in' => $ids,'post_type'=> 'parks'));
    global $post;
    foreach ($my_query as $post) {
       $posts_by_id[$post->ID] = $post;
    }
    foreach ($ids as $id) {
      if (!$post = $posts_by_id[$id]) continue;
       setup_postdata($post);
       echo '<p>TITLE: ';the_title();echo ' - ';the_ID(); '</p>';
       the_content();
    }
    Thread Starter gimperdaniel

    (@gimperdaniel)

    here’s what I just tried:

    query_posts(‘post_id=183&post_type=books’);?>

    if ( have_posts() ) : while ( have_posts() ) : the_post();

    the_post_thumbnail(array(70, 102));

    endwhile;endif;

    ————————-
    This will get the latest 10 books, it’s supposed to get one book.

    What could be wrong?

    1. Several query_posts on this page? some conflict?
    2. I use a plugin to regisger new post_types… is there any option that I need to set to true/false?

    This is a for a featured section on a books website:

    1. I setup the backend where the user can select the books from a drop down and pick which one they want to be featured on the main page.

    2. Then I save this in “options”. Just the post IDs. So it would look like something 2,5,10,1,11

    Then on the front page I get_option and break it into an array:

    array[0] = 2
    array[1] = 5

    and so on…

    And finally I could either do a query to query each post with a foreach loop

    Or I could use post__in (which is what I’m doing right now)

    the only problem with post__in is that it get’s my array and once it returns the values from the database it spits out in alphabetical or numerical order.

    so my array is 2,5,10,1,11
    the have_posts loops spits out 11,10,5,2,1

    I haven’t tried your solution for the post__in above.. will see if that works

    While testing, I found that if you misspell the post_type, that the query defaults back to querying all normal posts (at least for me in that one test). My custom type is ‘parks’, and I used ‘park’ in one test and got all posts.

    Seems like a bug to me – I would have expected no posts to be returned.

    Thread Starter gimperdaniel

    (@gimperdaniel)

    Yeah. I checked spelling many times. I have been trying to get this to work for over 5 hours.. started last night… but whenever i google post_types I get tons of tutorials on how to set it up.. but it looks like no one has the need to do what I’m trying to do.

    I am trying to use this code

    <?php
    $ids = array(548,555,587,583,585);
    $my_query = query_posts(array('post__in' => $ids,'post_type'=> 'parks'));
    global $post;
    foreach ($my_query as $post) {
       $posts_by_id[$post->ID] = $post;
    }
    foreach ($ids as $id) {
      if (!$post = $posts_by_id[$id]) continue;
       setup_postdata($post);
       echo '<p>TITLE: ';the_title();echo ' - ';the_ID(); '</p>';
       the_content();
    }
    ?>

    My ids are coming from a custom field set in the post, like DisplaysID = 1,2,3
    In my page I then do

    $DisplaysID  = get_custom_field("DisplaysID");
    $ids = array($DisplaysID);
    $my_query = query_posts(array('post__in' => $ids,'post_type'=> ''));

    without success….
    How can I use the value of my custom field to be used for the array to display the required posts?

    just found it…

    just need to do

    $ids = explode(',', $DisplaysID);

    so full code will be

    $DisplaysID  = get_custom_field("DisplaysID");
    $ids = explode(',', $DisplaysID);
    $my_query = query_posts(array('post__in' => $ids,'post_type'=> ''));

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘How to: Query a post (using post id) from a specific post_type?’ is closed to new replies.