• Resolved alexejp

    (@alexejp)


    hey there!

    i code a plugin.
    in this little piece of code i want to create an array which contains the title of each post as Key, this actually works great.
    but the post id is just not added as value to each key. Dont get the point in it, been looking for mistakes for ages, or at least felt like that.

    $heldengeschichtenDropdown = array( 'Platzhalter' => 0 );
    $args = array( 'category' => 5 );
    $heldengeschichten = get_posts( $args );
    foreach ( $heldengeschichten as $post ) : setup_postdata( $post );
    	$arraykey = get_the_title($post);
    	$value = $id;
    	$heldengeschichtenDropdown[$arraykey] = $value;
    endforeach;
    wp_reset_postdata();

    [Moderator note: code fixed. Please wrap code in the backtick character or use the code button.]

Viewing 4 replies - 1 through 4 (of 4 total)
  • @alexejp, try including global $post; in your code, per the Codex article for setup_postdata (there are examples in that article if you’re not sure where you should put it).

    $value = $post->ID;
    Thread Starter alexejp

    (@alexejp)

    Thanks very much for your help!

    including
    global $post;
    didn’t help. But
    $post->ID;
    makes the magic happen!

    that global $post; didn’t work quite surprised me pretty much.

    whats about this arrow -> notation? is it like $post is an object with attributes and one of these atts is ID?

    Moderator bcworkz

    (@bcworkz)

    is it like $post is an object with attributes and one of these atts is ID?

    Exactly! Though with objects, the values are usually referred to as ‘properties’ instead of ‘attributes’, but it’s just semantics.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Cannot get Post ID’ is closed to new replies.