WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

  <ul class="featured-jobs">
    $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
      '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>

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

What am I doing wrong here?

share|improve this question

Try using $wp_query global variable:

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

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

share|improve this answer

Your Answer


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.