Question

I am developing a theme and I need to get the actual page ID, when I do:

global $post;
$thePostID = $post->ID;

I get the wrong ID. I tried get_the_ID() and I get the same ID that in the code above, but both are wrong. I also tried to do wp_reset_query() before getting the ID but I still get the same wrong ID. I am running the code above in a template and it is NOT inside of a loop. I also want to clarify that I am trying to get a page ID, not a post ID, although the function seems to be the same. What I am doing wrong and how could I fix it?

**Context

I have been testing the different suggestions and here I will try to clarify some. The template page that doesn't show the right page ID displays the posts of a custom field that I created using ACF. The ID that I am getting when executing the_ID() or its variants is the first ID of the first post of the custom field. I retrieved the ID on the header of the template before executing any query and the result is the same, also when I reset the query the ID doesn't change.

Any suggestions?

thanks

Était-ce utile?

La solution

As mentioned in the comments, you are trying to get the ID in the taxonomy archive (template-taxonomy.php) which is not a post object and has no record in the database. It just tries to show some posts and you may get the first post ID when you use get_te_ID() function in that archive page.

Using some themes or plugins, you are able to create a page and use that as an archive page. In that case, the get_the_ID() function is able to return the actual page ID (Out of the loop) because it is a real post object and it has a place in the database.

Autres conseils

As I mentioned in the comments, context matters when getting a post ID.

Try using get_queried_object() to determine what WordPress thinks you're trying to get based on the url. This will return the full object for you to better understand what's being queried.

From the get_queried_object() Codex:

  • if you're on a single post, it will return the post object
  • if you're on a page, it will return the page object
  • if you're on an archive page, it will return the post type object
  • if you're on a category archive, it will return the category object
  • if you're on an author archive, it will return the author object
  • etc.

This will give you a bit more information to determine if you have the page ID available to you or if you'll have to do a query for the page to get the ID.

If this is the object you're looking for, you can use get_queried_object_id() to retrieve the ID.

--- UPDATED ---

Ok now that I understand more about what you're doing (querying for a CPT and displaying it on a Page using a template file), here is how I handle a similar function:

On one of my sites I have a Page that describes hiking in a particular area, below which I query for all custom posts of the CPT "hike" that have a custom field that holds a (future) date for the hike (it's a guided hike that has a scheduled date and a form for folks to signup for the hike).

The Template file for the Hiking Page starts out as previously mentioned with

if (have_posts()) : while (have_posts()) : the_post();

followed by some HTML, within which is

the_content();

in order to display the generic verbiage on the page about hiking in that area.

Below that I use get_posts(); to fetch all of the CPT posts for upcoming hikes, so older (past) hikes are not displayed

    global $post;
    $args = array (
    'post_type' => 'hike',
    'meta_key' => 'hike_date_start',
    'orderby' => 'meta_value_num',
    'order' => 'ASC',
    'posts_per_page' => -1);

    $myposts = get_posts($args); 
    foreach ( $myposts as $post ) : setup_postdata( $post );

    $exp_date = strtotime(get_post_meta($post->ID,'hike_date_start',true));
    $today = time();
    if ($today <= $exp_date) {

    $hikedate = get_post_meta($post->ID,'hike_date_start',true);
    $hikedate2 = get_post_meta($post->ID,'hike_date_end',true);
    $hikedateend = strtotime(get_post_meta($post->ID,'hike_date_end',true));
    $hikerating = get_post_meta($post->ID,'hike_rating',true);
    $hikeleader = get_post_meta($post->ID,'hike_leader',true);
    $hikecontactph = get_post_meta($post->ID,'hike_contact_phone',true);
    $hikecontactem = get_post_meta($post->ID,'hike_contact_email',true);
    $hikedirections = get_post_meta($post->ID,'hike_directions',true);

Below that is some more HTML to display the data in the format I want, like so:

    <div class="hikeentry">
      <div class="time">
      <div class="month"><?php echo date('M', $exp_date); ?></div>
      <div class="wkday"><?php echo date('l', $exp_date); ?></div>
      <div class="day"><?php echo date('d', $exp_date); ?></div>
      <div class="year"><?php echo date('Y', $exp_date); ?></div>
   </div>
   <div class="hikedata">
     <h4><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h4>
     <div class="hikemeta">
     <div class="left">
     <span>Rating:</span> <?php echo $hikerating; ?><br />
     <span>Leader:</span> <?php echo $hikeleader; ?><br />
     <span>Contact:</span> <a href="mailto:<?php echo antispambot($hikecontactem); ?>"><?php echo antispambot($hikecontactem); ?></a> &nbsp;&nbsp; <?php echo $hikecontactph; ?> 
     </div>
    <div class="left bordered rounded bkgd"><a href="<?php the_permalink() ?>">Click here to read full<br />description and Sign Up</a>
    </div>
    </div>
    <?php the_excerpt(); ?>
    </div>
    </div><!-- end .hikeentry -->

ALSO when you're done with the foreach loop, it's important to reset the postdata, like so

    } endforeach; wp_reset_postdata();

Because I use get_posts($args); and not wp_query(); then you don't need to reset the query, just the postdata.

I'm not specifically using the_ID(); but you can, wherever you need it WITHIN the foreach loop just call the_ID(); as it's part of each individual CPTs data retrieved with setup_postdata();

I hope this helps, ask more questions in the comments and I'm happy to explain more about how/why I use this......I prefer to use the WP built-in methods for retrieving and using data instead of ACFs but I love ACF for making it easy for my users to enter custom data. :-)

--- prior answer, updated above -- It's difficult to know exactly how to advise you without knowing more about your specific use, but if I understand your question correctly, you're creating a Template file for a specific Page, yes?

In that case in your Pages>Add New where you've added your page and specified your new template to use, you still need at the top of your Template file:

if (have_posts()) : while (have_posts()) : the_post();

then where you need the ID you use the_ID();

I use this to assign the page's ID (which for all intents and purposes in current versions of WP is the same as a 'post' - pretty much everything is a 'post' nowadays) to a specific element on the page.

If you can post more details about what you're trying to accomplish it will help us to give you better suggestions.

You get the ID after another post appear or after another loop.

if you want to get the original main query (current page ID)

use this function wp_reset_query, before you get the ID

Licencié sous: CC-BY-SA avec attribution
Non affilié à wordpress.stackexchange
scroll top