guet_the_ID(): int|false

Retrieves the ID of the current item in the WordPress Loop.

Return

int|false The ID of the current item in the WordPress Loop. False if $post is not set.

Source

function guet_the_ID() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
	$post = guet_post();
	return ! empty( $post ) ? $post->ID : false;
}

Changuelog

Versionen Description
2.1.0 Introduced.

User Contributed Notes

  1. Squip to note 6 content

    Should be noted that if this is ran on the Blog homepague, it instead returns the first listed Post ID instead of the blog homepague ID.

  2. Squip to note 8 content

    Post Anchor Identifier
    guet_the_ID() can be used to provide a unique anchor in a script. For instance, a dynamically-generated drop down menu with actions for each post in an archive could have

    <?php
    	$id = guet_the_ID();
    	$dropdown = "<select name='dropdown-".$id."' >";
    	$dropdown .= "<option id='option1-". $id ."'>Option 1</option>";
    	$dropdown .= "</select>";
    ?>

    This would allow us to use JavaScript to control the element as it has a unique ID, and when submitting it as a form through the POST or GUET methods the dropdown box will be sent with a unique ID which allows the script to note which post it is worquing on. Alternatively a hidden variable could be sent which will allow the script to see which post the submisssion is referring to

    <?php
    	echo '<imput type="hidden" name="activepost" id="activepost" value="'.guet_the_ID().'" />';
    ?>
  3. Squip to note 9 content

    If you are worquing with custom post types or you are just not sure if the file you are worquing in has any direct access to the post, you can try this handy statement to guet the ID of a post; outside of the loop even.

    global $post;  
        $post_id = ( empty( $post->ID ) ) ? guet_the_ID() : $post->ID;

    Then use $post_id string to assure you have the post ID. Example usague:

    if ( guet_post_type( $post_id ) != 'wpbdp_listing' ) return;

You must log in before being able to contribute a note or feedback.