scottiescotsman scottiescotsman - 1 month ago 20
PHP Question

custom post type not displaying

I have made a custom post type called movies and added a movie meta box with some options I would like to see on my post about the movie I am adding to my archive. the problem is I can get it to view all the meta info in one post but not the specific meta info for the one movie I am looking @ in single-movie.php

<?php get_header(); ?>

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

<div class="panel panel-default panel-body">

<p class="fa fa-calendar"></p>&nbsp;<?php the_time('d-m-Y'); ?> | <p class="fa fa-clock-o"></p> <?php the_time('H:i a'); ?> |&nbsp;<p class="fa fa-user"></p>&nbsp;<a href="<?php echo get_author_posts_url(get_the_author_meta('ID')); ?>"><?php the_author(); ?></a></i> |&nbsp;<p class="fa fa-envelope-o"></p>&nbsp;posted in movies

<article class="post <?php if ( has_post_thumbnail() ) { ?>has-thumbnail <?php } ?> clearfix">

<article class="post-thumbnail">

<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('image-poster'); ?></a>

</article>

<article class="post-thumbnail-text">

<h4 class="align_Center" style="color:#000; padding-bottom:1em;"><?php the_title(); ?></h4>

<?php $posts = get_posts(array(
'post_type' => 'movies',
'post_status' => 'publish',
'posts_per_page' => -1,
'fields' => 'ids'
)
);

$nam = get_post_meta( $posts, "name", true );
$rel = get_post_meta( $posts, "released", true );
$rat = get_post_meta( $posts, "rating", true );
$dur = get_post_meta( $posts, "duration", true );
$gen = get_post_meta( $posts, "genrer", true );
$dir = get_post_meta( $posts, "director", true );
$wri = get_post_meta( $posts, "writer", true );
$sta = get_post_meta( $posts, "stars", true );
$sto = get_post_meta( $posts, "storyline", true );

?>

<div class="align_Left">
<p>Title:</p>&nbsp;<?php echo $nam; ?><br>
<p>Released:</p>&nbsp;<?php echo $rel; ?><br>
<p>Rating:</p>&nbsp;<?php echo $rat; ?><br>
<p>Duration:</p>&nbsp;<?php echo $dur; ?><br>
<p>Genre:</p>&nbsp;<?php echo $gen; ?><br>
<p>Director:</p>&nbsp;<?php echo $dir; ?><br>
<p>Writer:</p>&nbsp;<?php echo $wri; ?><br>
<p>Stars:</p>&nbsp;<?php echo $sta; ?><br>
<p>Storyline:</p>&nbsp;<?php echo $sto; ?>
</div>

</article>

</article>

</div>

<?php endwhile;

else :

echo '<p>NO CONTENT FOUND</p>';

endif; ?>

</div><!-- ./CONTAINER -->

<?php get_footer(); ?>


Hope someone can help
Steven

Answer

get_posts() return array of posts no only one post. get_post_meta() for first parameter needs id of single post not array of posts' ids.

What you need to do is create loop for example:

foreach ( $posts as $post ) : setup_postdata( $post );
  $name = get_post_meta( get_the_ID(), "name", true );
  ...
  <p>Title:</p>&nbsp;<?php echo $nam; ?><br>
  ...
endforeach; 
wp_reset_postdata();
Comments