Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying the following (it's a sidebar widget in the Single New template):

  <ul class="featured-jobs">
  <?php
    $postid = $post->ID;
    $categories = get_the_category($post->ID);
  ?>
  <h2><?php echo $postid; ?></h2>
  <h2><?php echo $categories; ?></h2>
  <?php // Create and run custom loop
    $args=array(
      'post_type' => 'news',
      'category__in' => $categories,
      'post__not_in' => $postid,
      'posts_per_page'=> 3 // Number of related posts that will be shown.
    );
    $custom_posts = new WP_Query();
    $custom_posts->query( $args );
    while ($custom_posts->have_posts()) : $custom_posts->the_post();
  ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
  <?php endwhile; ?>
  <?php wp_reset_postdata(); ?>
  <li class="see-all-positions"><a href="http://www.pixelmatic.com/open-jobs/">See All Positions >></a></li>
  </ul>

But as it turns out, $postid comes up blank and $categories outputs as Array.

What am I doing wrong here?

share|improve this question
add comment

2 Answers

Try using $wp_query global variable:

global $wp_query;
$postid = $wp_query->post->ID;
share|improve this answer
add comment
$_post = get_queried_object();
$post_id = $_post->ID;

It's somehow like that how single_post_title() gets the title.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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