0

In my theme, I have a custom post type called sliders. Here the user is allowed to upload images in a meta-box. So, the images are saved as meta data in wp_post_meta table.

Now what I want to do is, displaying a slider using its ID.

I did like following but no result

$my_query = new WP_Query('post_type=sliders&p=411');

if( $my_query->have_posts() ) {
   while ($my_query->have_posts()) : $my_query->the_post(); 
   the_post();
 endwhile;
}

This doesn't display anything. No even errors. Anyway, If I used the_title() instead of the_post(), it shows the title of the slider fine. Same for the the_author() It shows the author without error.

Why is this weird ?

2 Answers 2

1

EDIT: deleted incorrect information

I see what you are missing now. Without knowing the specifics, I tried to point you in the right direction with this:

$my_query = new WP_Query('post_type=sliders&p=411');

if( $my_query->have_posts() ) {
    while ( $my_query->have_posts() ) : $my_query->the_post();

        // Get the specific meta data for the current post
        $saved_slider_meta = get_post_meta( get_the_ID(), 'slider_meta_key' );

        // Echo the data
        echo $saved_slider_meta;

    endwhile;

    // Restore original post data if there are other loops
    wp_reset_post_data();
}

Here is more information about WP_Query.

2
  • That string syntax is correct in context Commented Feb 6, 2016 at 18:43
  • Thanks so I can't get the post directly. I have to call meta details seperately... :) OK I'll try that!! Thanks! Commented Feb 6, 2016 at 19:39
1
$my_query = new WP_Query('post_type=sliders&p=411');
if( $my_query->have_posts() ) {
  while ( $my_query->have_posts() ) : $my_query->the_post();

    $slider_imgs = get_post_meta( get_the_ID(), 'slider_meta_key' );

    foreach($slider_imgs as $slider_img)
    {
       echo '<img src="'.$saved_slider_meta.'">';
    }
  endwhile;
  wp_reset_post_data();
}

Check documentation of WP_Query and get_post_meta

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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