Php tutorial page id

How to Get WordPress Post and Page IDs (Code Snippets)

The post ID in WordPress can be used for tons of reasons. Whether it’s for a plugin or custom code here are 6 ways to get WordPress post ID.

Post and page IDs are used in WordPress to identify the piece of content on your site. Since titles, content, and other data can change WordPress uses these IDs as unique identifiers for your content. Many plugins will allow you to use a post ID in their functionality, for example excluding a post from a function. Additionally if you’re a WordPress developer getting the ID of a post or page can be used for various functions.

Here are 6 ways to get a post ID in WordPress:

1. Find Post ID in WordPress Dashboard

The easiest way to get the ID of a post in WordPress is to login to your site and edit a post. While editing a post in WordPress you can see it’s post ID in the URL of your browser.

Читайте также:  Php varchar to int

This can be done by heading to Posts>all posts .

Here you can click “edit” on the post you want to see the post ID of.

After selecting a post you’ll be able to see the post ID in the browser URL bar.

This is great for one-off use cases like getting a post or page ID for use in a plugin’s function.

2. View Post IDs using WordPress Plugin

If you just want to see your post IDs in your WordPress dashboard as a column you can use the plugin Reveal IDs.

After enabling the plugin you’ll see an ID column in your WordPress dashboard for all post types.

3. Get Current Post ID (using PHP)

Want to get the current post ID of the current post? Using the get_the_ID() function will easily get post ID. This works in template files or inside any loop.

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

echo ‘The current post ID is: ‘ .get_the_ID();

You can also use the the_ID() function to install echo the current post’s ID.

4. Get Post ID by Slug (using PHP)

The get_page_by_path() function allows you to get post data in WordPress using the post slug. So in this example we will be getting the post ID from its slug.

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

$ post_data = get_page_by_path( ‘my-post-slug’ );
$ post_id = $ post_data -> ID ;
if (!empty( $ post_id ))
echo ‘The post ID is: ‘ . $ post_id ;
>

5. Get Post ID by Title (using PHP)

Using the get_page_by_title() function allows us to get a post’s ID by its title. Just make sure to specify which post type you’re trying to retrieve as seen below (post/page).

This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters

$ post_details = get_page_by_title( ‘My Post Title’ , » , ‘post’ );
echo $ post_details -> ID ;
$ page_details = get_page_by_title( ‘My Page Title’ , » , ‘page’ );
echo $ page_details -> ID ;

6. Get Post ID by Full URL (using PHP)

Using url_to_postid() allows us to get a post’s ID by using its URL.

Источник

Php wordpress how to get page id

Below that I use to fetch all of the CPT posts for upcoming hikes, so older (past) hikes are not displayed Below that is some more HTML to display the data in the format I want, like so: ALSO when you’re done with the foreach loop, it’s important to reset the postdata, like so Because I use and not then you don’t need to reset the query, just the postdata. 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: then where you need the ID you use 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.

Is_page() function not displaying content to specific page ID’s

Try the below code that will work properly.

if( is_page(96) || is_page(98) || is_page(61) ) : ?> 
$id = get_the_ID(); if( ($id == 96) || ($id == 98) || ($id == 61) ) : ?> 

You can use get_the_ID(); or $post->ID; . you will get the ID of the current post of the loop

$ids = [96, 78]; if(in_array(get_the_ID(), $ids)) echo '
';

Please take a look at the docs :

How to get the specific Page's current ID and use it in a get_page, The id will be embedded in the query string of the URL, e.g. page.php?action=edit&post=123. $page_data = get_page( $page_id ); // You must pass

How to Get Post and Page IDs in WordPress

On the hunt for a specific page or post's ID? Look no further. This guide has 5 easy ways to Duration: 6:20

Getting the wrong page ID

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.

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.

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

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 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:

">
Rating:
Leader:
Contact: ">   
">Click here to read full
description and Sign Up

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.

Get page id by template - WordPress Development Stack Exchange, $page_details = get_pages( array( 'post_type' => 'page', 'meta_key' => '_wp_page_template', 'hierarchical' => 0, 'meta_value' => 'page-templates

How to get the page id in custom plugin of wordpress

global $post; $page_ID = $post->ID; var_dump($page_ID); 

Get page Id from slug in wordpress, $page = get_page_by_path("page-slug", OBJECT, 'page');. But it returns media attachment instead of page. I only wants the pages not any other

Источник

Оцените статью