SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    328
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    How to query post by post id

    Hi guys,
    I want to show a specific wordpress post in a modal popup. I've retrieve the post id via ajax, but when I query for the post with the code bellow it throws error:
    Code:
    query_posts(array(
    	'post__in' => $post_id
    ));
    if ( have_posts() ) :
    	while ( have_posts() ) : the_post();
    		the_content();
    	endwhile;
    endif;
    My Google search suggests that I should do the query this way. But why it isn't working?

    Thank you,

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,271
    Mentioned
    197 Post(s)
    Tagged
    3 Thread(s)
    Is post__in supposed to have 2 underscores?
    What is the error message?

  3. #3
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    328
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Thank you,
    I got it. It should be:
    Code:
    query_posts(array(
    	'post__in' => array($post_id)
    ));
    if ( have_posts() ) :
    	while ( have_posts() ) : the_post();
    		the_content();
    	endwhile;
    endif;

  4. #4
    SitePoint Wizard silver trophybronze trophy asp_funda's Avatar
    Join Date
    Jun 2003
    Location
    ether
    Posts
    4,497
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Instead of doing that, use get_post(). Using query_posts() like that is a bad idea and can cause unwanted side effects (because it will change your main WordPress query for that particular page & you need to remember to reset it by calling wp_reset_query() after your block of code). Which is why it is better to use get_post() for a single post (where you know post ID) & get_posts() for multiple posts.
    Our lives teach us who we are.
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Me - Photo Blog - Personal Blog - Dev Blog
    iG:Syntax Hiliter -- Colourize your code in WordPress!!

  5. #5
    SitePoint Member
    Join Date
    Jun 2014
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi guy, I've found this plugin: Content Views http://wordpress.org/plugins/content...lay-post-page/
    It is easy to query post(s) by id, without coding!
    It even offers you more features.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •