WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to get post ID? (4 posts)

  1. ardyonline
    Member
    Posted 4 years ago #

    How to get all post ID in a certain category? and put the result in a array variable?

    $var = array(1,6,11);

    where 1, 6, 11 are the postID of specific category.

  2. MichaelH
    Member
    Posted 4 years ago #

    Something like:

    <?php
    $cat = 47; //category id
    $posts=get_posts('showposts=-1&cat='. $cat);
    if ($posts) {
    foreach($posts as $post) {
    echo "<pre>"; print_r($post->ID); echo "</pre>";
    }
    }
    ?>
  3. ardyonline
    Member
    Posted 4 years ago #

    thanks MichaelH!
    it enlightens me, it really helps!

  4. GandJ
    Member
    Posted 4 years ago #

    Below is the exact code to get it in the array format,

    <?php
            $cat = 47; //category id
            $posts=get_posts('showposts=-1&cat='. $cat);
    	if ($posts) {
    	 $cat_post_ids = array();
    	foreach($posts as $post) {
    		array_push($cat_post_ids, $post->ID);
    	}
    	print_r($cat_post_ids);
    	}
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic