WordPress.org

Ready to get started?Download WordPress

Forums

Query_Posts Does Not Work If Post_type is not Post (4 posts)

  1. gimperdaniel
    Member
    Posted 3 years ago #

    All I need and this is not too much to ask is to Query ONE post.

    Problem:

    It's not a "post" post_type. It's a custom post_type named "books".

    Query:

    Post id to get = 10;

    query_posts('p=10'); <= THIS DOESN'T WORK
    query_posts('post_id'=>10) <= THIS DOESN'T WORK

    I think I know why. because the function query_posts only searches for posts that are have the "post" post_type.

    So I thought.. hey I know how to fix that.

    query_posts('post_type' => 'books', 'p'=>10); <= THAT DOESN'T WORK
    The query above will query all posts in 'books' and ignores the post id.

    Please, can someone share how they use query posts to query just one post from a custom post_type?

  2. vtxyzzy
    Member
    Posted 3 years ago #

    query_posts('p=10'); <= THIS DOESN'T WORK

    Nope, because the 'p=10' asks for a normal post with id=10.

    query_posts('post_id'=>10) <= THIS DOESN'T WORK

    Nope, because this asks for a normal page with id=10.

    query_posts('post_type' => 'books', 'p'=>10); <= THAT DOESN'T WORK

    Nope, because 'p' => 10 asks for a normal post with id=10, overriding the post_type.

    This works for me (with a different id and post_type):

    query_posts(array('post__in' => array(548), 'post_type' => 'parks'));

    Strangely, this does not work:

    query_posts('post_id=548&post_type=parks');  // Gets ALL parks
  3. gimperdaniel
    Member
    Posted 3 years ago #

    That's what i've been telling you.. nothing works.. unless you write your own query... but i just hate mysql "joins"

  4. vtxyzzy
    Member
    Posted 3 years ago #

    Sorry, I didn't get what you were saying. Is this writing your own query (no joins in sight)?

    query_posts(array('post__in' => array(548), 'post_type' => 'parks'));

Topic Closed

This topic has been closed to new replies.

About this Topic