Johann
Johann

Reputation: 691

Wordpress query specific post

I've created a "portafolio" custom type in Wordpress and I would like to query a specific post (ID = 780 to be exact) in this code I've written, which retrieves all attachments images inside that post:

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>


    <?php if ( $post->post_type == 'portafolio' && $post->post_status == 'publish' ) {
            $attachments = get_posts( array(
                'post_type' => 'attachment',
                'posts_per_page' => -1,
                'post_parent' => $post->ID,
                'exclude'     => get_post_thumbnail_id(),
                'orderby'           => 'menu_order',
                'order'           => 'ASC'  

    ) );?>

    <?php
            if ( $attachments ) {
                foreach ( $attachments as $attachment ) {
                    $class = "post-attachment mime-" . sanitize_title( $attachment->post_mime_type );
                    $thumbimg = wp_get_attachment_image_src( $attachment->ID, 'original', false ); ?>


    {image : '<?php echo '' . $thumbimg[0] . ''; ?>', title : '<?php wp_title("",true); ?>', thumb : ''},

    <?php               
                }

            }
        }
    ?>


<?php endwhile; endif; ?>

However, I don't know where in that code I should query the specific post I want to retrieve.

Any ideas?

EDIT:

I have tried:

<?php
    query_posts( array('p' => 780) );
    if (have_posts()) : while (have_posts()) : the_post();
    ?>

    <h2>Test</h2>

    <?php endwhile; endif; ?>

Just to see if I was able to retrieve something, without luck.

If this helps, I am doing this so I can create a page on Wordpress to display a single post of a custom type called "portafolio" and then assign it as frontpage.

Upvotes: 0

Views: 2450

Answers (2)

martinczerwi
martinczerwi

Reputation: 2847

You can change your query by calling query_posts before the loop:

query_posts( array('p' => $myPostID) );

Simply call this function before your loop. Your final code could be like this:

<?php
query_posts( array('p' => 780) );
if (have_posts()) : while (have_posts()) : the_post();
?>

You can specify even more parameters, like authors, or post-types, etc. Call rewind_posts() after endwhile; endif;, if you have another loop after your main loop.

EDIT:

A few comments later... be sure to add 'post_type' to your function, if you're working with custom post types. For example:

query_posts( array('p' => 780, 'post_type' => 'book') );

Upvotes: 1

soju
soju

Reputation: 25322

You can simply use get_post :

$my_id = 780;
$my_post = get_post($my_id);

Upvotes: 1

Related Questions