Làm cách nào để tìm URL của ID bài đăng WordPress?

Tôi quyết định tạo hướng dẫn siêu chi tiết này để bạn không bao giờ gặp vấn đề khi tìm ra bất kỳ ID bài đăng nào. Khi tôi nói “ID bài đăng”, tôi cũng có nghĩa là các trang, loại bài đăng tùy chỉnh cũng như các sản phẩm và đơn đặt hàng WooC Commerce

Chỉ trong trường hợp bạn thấy mình trên trang này đang tìm kiếm một thông tin ngược lại về cách lấy một bài đăng theo ID, thì tất cả những gì bạn cần làm là sử dụng các hàm

/**
 * Add Post ID Column to WordPress Admin
 *
 * @author Misha Rudrastyh
 * @link https://rudrastyh.com/wordpress/get-post-id.html#post_id_column
 */
add_filter( 'manage_posts_columns', 'misha_add_column' );
add_action( 'manage_posts_custom_column', 'misha_column_content', 5, 2 );
// for Pages:
// add_filter( 'manage_pages_columns', 'misha_add_column' );
// add_action( 'manage_pages_custom_column', 'misha_column_content', 5, 2 );
function misha_add_column( $columns ){
	$columns[ 'misha_post_id' ] = 'ID';
	return $columns;
}
function misha_column_content( $column, $post_id ){
	if( 'misha_post_id' === $column ) {
		echo $post_id;
	}
}
1, nó thực sự đơn giản, như thế này

$post = get_post( 52 );
// let's get a post title by ID
echo $post->post_title;

Bây giờ, hãy đi sâu vào 14 phương pháp lấy ID bài đăng của chúng tôi. )

1. Trong URL khi bạn chỉnh sửa bài đăng

Đó thực sự là cách đơn giản nhất. Nếu bạn không sử dụng ID mới tìm thấy ở đâu đó trong mã, bạn chỉ có thể tìm thấy nó trong URL khi bạn chỉnh sửa bài đăng hoặc trang cụ thể này

Làm cách nào để tìm URL của ID bài đăng WordPress?

Nó thậm chí không bắt buộc phải mở trang chỉnh sửa bài đăng. Chỉ cần di chuột qua liên kết «Chỉnh sửa» hoặc qua tiêu đề bài đăng trong khu vực quản trị và sau đó nhìn vào thanh trạng thái của trình duyệt (phần dưới cùng bên trái của màn hình)

2. Thêm một cột tùy chỉnh với ID bài đăng

tôi thích phương pháp này. Nếu bạn làm việc với ID bài đăng rất thường xuyên, nó sẽ rất hữu ích cho bạn

Làm cách nào để tìm URL của ID bài đăng WordPress?
Bạn cũng có thể thêm cột này cho “Trang” hoặc các Loại bài đăng tùy chỉnh khác

Tất cả những gì bạn phải làm là sử dụng mã sẵn sàng sử dụng bên dưới. Nếu bạn không biết chèn nó vào đâu, tôi khuyên bạn nên đọc hướng dẫn này

/**
 * Add Post ID Column to WordPress Admin
 *
 * @author Misha Rudrastyh
 * @link https://rudrastyh.com/wordpress/get-post-id.html#post_id_column
 */
add_filter( 'manage_posts_columns', 'misha_add_column' );
add_action( 'manage_posts_custom_column', 'misha_column_content', 5, 2 );
// for Pages:
// add_filter( 'manage_pages_columns', 'misha_add_column' );
// add_action( 'manage_pages_custom_column', 'misha_column_content', 5, 2 );
function misha_add_column( $columns ){
	$columns[ 'misha_post_id' ] = 'ID';
	return $columns;
}
function misha_column_content( $column, $post_id ){
	if( 'misha_post_id' === $column ) {
		echo $post_id;
	}
}

3-5. Nhận ID bài đăng trong một vòng lặp

Như bạn có thể biết, có thể có nhiều loại vòng lặp khác nhau, ví dụ: chúng ta có thể sử dụng

/**
 * Add Post ID Column to WordPress Admin
 *
 * @author Misha Rudrastyh
 * @link https://rudrastyh.com/wordpress/get-post-id.html#post_id_column
 */
add_filter( 'manage_posts_columns', 'misha_add_column' );
add_action( 'manage_posts_custom_column', 'misha_column_content', 5, 2 );
// for Pages:
// add_filter( 'manage_pages_columns', 'misha_add_column' );
// add_action( 'manage_pages_custom_column', 'misha_column_content', 5, 2 );
function misha_add_column( $columns ){
	$columns[ 'misha_post_id' ] = 'ID';
	return $columns;
}
function misha_column_content( $column, $post_id ){
	if( 'misha_post_id' === $column ) {
		echo $post_id;
	}
}
2,
/**
 * Add Post ID Column to WordPress Admin
 *
 * @author Misha Rudrastyh
 * @link https://rudrastyh.com/wordpress/get-post-id.html#post_id_column
 */
add_filter( 'manage_posts_columns', 'misha_add_column' );
add_action( 'manage_posts_custom_column', 'misha_column_content', 5, 2 );
// for Pages:
// add_filter( 'manage_pages_columns', 'misha_add_column' );
// add_action( 'manage_pages_custom_column', 'misha_column_content', 5, 2 );
function misha_add_column( $columns ){
	$columns[ 'misha_post_id' ] = 'ID';
	return $columns;
}
function misha_column_content( $column, $post_id ){
	if( 'misha_post_id' === $column ) {
		echo $post_id;
	}
}
3 hoặc
/**
 * Add Post ID Column to WordPress Admin
 *
 * @author Misha Rudrastyh
 * @link https://rudrastyh.com/wordpress/get-post-id.html#post_id_column
 */
add_filter( 'manage_posts_columns', 'misha_add_column' );
add_action( 'manage_posts_custom_column', 'misha_column_content', 5, 2 );
// for Pages:
// add_filter( 'manage_pages_columns', 'misha_add_column' );
// add_action( 'manage_pages_custom_column', 'misha_column_content', 5, 2 );
function misha_add_column( $columns ){
	$columns[ 'misha_post_id' ] = 'ID';
	return $columns;
}
function misha_column_content( $column, $post_id ){
	if( 'misha_post_id' === $column ) {
		echo $post_id;
	}
}
4 để tạo vòng lặp. Hãy thử in ID bài đăng cho hai trong số các tình huống này, bởi vì
/**
 * Add Post ID Column to WordPress Admin
 *
 * @author Misha Rudrastyh
 * @link https://rudrastyh.com/wordpress/get-post-id.html#post_id_column
 */
add_filter( 'manage_posts_columns', 'misha_add_column' );
add_action( 'manage_posts_custom_column', 'misha_column_content', 5, 2 );
// for Pages:
// add_filter( 'manage_pages_columns', 'misha_add_column' );
// add_action( 'manage_pages_custom_column', 'misha_column_content', 5, 2 );
function misha_add_column( $columns ){
	$columns[ 'misha_post_id' ] = 'ID';
	return $columns;
}
function misha_column_content( $column, $post_id ){
	if( 'misha_post_id' === $column ) {
		echo $post_id;
	}
}
4 không phải là thứ mà bạn nên sử dụng thường xuyên

get_posts()

$articles = get_posts( array( 'posts_per_page' => 50 ) );
foreach( $articles as $article ) {
	echo $article->ID;
}

Như bạn có thể thấy theo mặc định, hàm này trả về mảng đối tượng WP_Post, nhưng nó cũng có thể trả về mảng ID bài đăng, bạn đặt tham số

/**
 * Add Post ID Column to WordPress Admin
 *
 * @author Misha Rudrastyh
 * @link https://rudrastyh.com/wordpress/get-post-id.html#post_id_column
 */
add_filter( 'manage_posts_columns', 'misha_add_column' );
add_action( 'manage_posts_custom_column', 'misha_column_content', 5, 2 );
// for Pages:
// add_filter( 'manage_pages_columns', 'misha_add_column' );
// add_action( 'manage_pages_custom_column', 'misha_column_content', 5, 2 );
function misha_add_column( $columns ){
	$columns[ 'misha_post_id' ] = 'ID';
	return $columns;
}
function misha_column_content( $column, $post_id ){
	if( 'misha_post_id' === $column ) {
		echo $post_id;
	}
}
6

$articles = get_posts( array( 'posts_per_page' => 50, 'fields' => 'ids' ) );
foreach( $articles as $article_id ) {
	echo $article_id;
}

WP_Query

$post = get_post( 52 );
// let's get a post title by ID
echo $post->post_title;
0

Vì vậy, toàn bộ 3 cách để bạn có thể nhận ID bài đăng trong một vòng lặp được tạo bằng

/**
 * Add Post ID Column to WordPress Admin
 *
 * @author Misha Rudrastyh
 * @link https://rudrastyh.com/wordpress/get-post-id.html#post_id_column
 */
add_filter( 'manage_posts_columns', 'misha_add_column' );
add_action( 'manage_posts_custom_column', 'misha_column_content', 5, 2 );
// for Pages:
// add_filter( 'manage_pages_columns', 'misha_add_column' );
// add_action( 'manage_pages_custom_column', 'misha_column_content', 5, 2 );
function misha_add_column( $columns ){
	$columns[ 'misha_post_id' ] = 'ID';
	return $columns;
}
function misha_column_content( $column, $post_id ){
	if( 'misha_post_id' === $column ) {
		echo $post_id;
	}
}
3

  1. Hàm
    /**
     * Add Post ID Column to WordPress Admin
     *
     * @author Misha Rudrastyh
     * @link https://rudrastyh.com/wordpress/get-post-id.html#post_id_column
     */
    add_filter( 'manage_posts_columns', 'misha_add_column' );
    add_action( 'manage_posts_custom_column', 'misha_column_content', 5, 2 );
    // for Pages:
    // add_filter( 'manage_pages_columns', 'misha_add_column' );
    // add_action( 'manage_pages_custom_column', 'misha_column_content', 5, 2 );
    function misha_add_column( $columns ){
    	$columns[ 'misha_post_id' ] = 'ID';
    	return $columns;
    }
    function misha_column_content( $column, $post_id ){
    	if( 'misha_post_id' === $column ) {
    		echo $post_id;
    	}
    }
    8 sử dụng biến toàn cục
    /**
     * Add Post ID Column to WordPress Admin
     *
     * @author Misha Rudrastyh
     * @link https://rudrastyh.com/wordpress/get-post-id.html#post_id_column
     */
    add_filter( 'manage_posts_columns', 'misha_add_column' );
    add_action( 'manage_posts_custom_column', 'misha_column_content', 5, 2 );
    // for Pages:
    // add_filter( 'manage_pages_columns', 'misha_add_column' );
    // add_action( 'manage_pages_custom_column', 'misha_column_content', 5, 2 );
    function misha_add_column( $columns ){
    	$columns[ 'misha_post_id' ] = 'ID';
    	return $columns;
    }
    function misha_column_content( $column, $post_id ){
    	if( 'misha_post_id' === $column ) {
    		echo $post_id;
    	}
    }
    9 được cài đặt từ một đối tượng có sẵn trong thuộc tính
    $articles = get_posts( array( 'posts_per_page' => 50 ) );
    foreach( $articles as $article ) {
    	echo $article->ID;
    }
    0
  2. $articles = get_posts( array( 'posts_per_page' => 50 ) );
    foreach( $articles as $article ) {
    	echo $article->ID;
    }
    1 cũng tương tự nhưng nó trả về kết quả không in ra
  3. $articles = get_posts( array( 'posts_per_page' => 50 ) );
    foreach( $articles as $article ) {
    	echo $article->ID;
    }
    2 chỉ là ID bài đăng được lấy trực tiếp từ một đối tượng

6. Nhận ID bài đăng của bài đăng hiện tại (chính xác không phải trong một vòng lặp)

Hãy tưởng tượng một tình huống khi bạn mở tệp

$articles = get_posts( array( 'posts_per_page' => 50 ) );
foreach( $articles as $article ) {
	echo $article->ID;
}
3 (hoặc
$articles = get_posts( array( 'posts_per_page' => 50 ) );
foreach( $articles as $article ) {
	echo $article->ID;
}
4, v.v.) và bạn chỉ cần lấy ID bài đăng trong đó

Có ba cách để bạn có thể làm điều đó

Cách thứ nhất có lẽ mình không khuyến khích, nhưng bạn vẫn phải biết

$post = get_post( 52 );
// let's get a post title by ID
echo $post->post_title;
9

Cách thứ hai là sử dụng

/**
 * Add Post ID Column to WordPress Admin
 *
 * @author Misha Rudrastyh
 * @link https://rudrastyh.com/wordpress/get-post-id.html#post_id_column
 */
add_filter( 'manage_posts_columns', 'misha_add_column' );
add_action( 'manage_posts_custom_column', 'misha_column_content', 5, 2 );
// for Pages:
// add_filter( 'manage_pages_columns', 'misha_add_column' );
// add_action( 'manage_pages_custom_column', 'misha_column_content', 5, 2 );
function misha_add_column( $columns ){
	$columns[ 'misha_post_id' ] = 'ID';
	return $columns;
}
function misha_column_content( $column, $post_id ){
	if( 'misha_post_id' === $column ) {
		echo $post_id;
	}
}
8 và
$articles = get_posts( array( 'posts_per_page' => 50 ) );
foreach( $articles as $article ) {
	echo $article->ID;
}
6. Nó khá ổn, nó được sử dụng rộng rãi, cũng hoạt động trên cơ sở biến toàn cầu
/**
 * Add Post ID Column to WordPress Admin
 *
 * @author Misha Rudrastyh
 * @link https://rudrastyh.com/wordpress/get-post-id.html#post_id_column
 */
add_filter( 'manage_posts_columns', 'misha_add_column' );
add_action( 'manage_posts_custom_column', 'misha_column_content', 5, 2 );
// for Pages:
// add_filter( 'manage_pages_columns', 'misha_add_column' );
// add_action( 'manage_pages_custom_column', 'misha_column_content', 5, 2 );
function misha_add_column( $columns ){
	$columns[ 'misha_post_id' ] = 'ID';
	return $columns;
}
function misha_column_content( $column, $post_id ){
	if( 'misha_post_id' === $column ) {
		echo $post_id;
	}
}
9

Nhưng yêu thích của tôi là cái này

/**
 * Add Post ID Column to WordPress Admin
 *
 * @author Misha Rudrastyh
 * @link https://rudrastyh.com/wordpress/get-post-id.html#post_id_column
 */
add_filter( 'manage_posts_columns', 'misha_add_column' );
add_action( 'manage_posts_custom_column', 'misha_column_content', 5, 2 );
// for Pages:
// add_filter( 'manage_pages_columns', 'misha_add_column' );
// add_action( 'manage_pages_custom_column', 'misha_column_content', 5, 2 );
function misha_add_column( $columns ){
	$columns[ 'misha_post_id' ] = 'ID';
	return $columns;
}
function misha_column_content( $column, $post_id ){
	if( 'misha_post_id' === $column ) {
		echo $post_id;
	}
}
3

Nó trả về ID của bất kỳ loại đối tượng nào mà trang đang hiển thị ngay bây giờ. Vì vậy, nó cũng có thể được sử dụng để lấy ID danh mục hoặc thẻ hiện tại

Đủ đơn giản

/**
 * Add Post ID Column to WordPress Admin
 *
 * @author Misha Rudrastyh
 * @link https://rudrastyh.com/wordpress/get-post-id.html#post_id_column
 */
add_filter( 'manage_posts_columns', 'misha_add_column' );
add_action( 'manage_posts_custom_column', 'misha_column_content', 5, 2 );
// for Pages:
// add_filter( 'manage_pages_columns', 'misha_add_column' );
// add_action( 'manage_pages_custom_column', 'misha_column_content', 5, 2 );
function misha_add_column( $columns ){
	$columns[ 'misha_post_id' ] = 'ID';
	return $columns;
}
function misha_column_content( $column, $post_id ){
	if( 'misha_post_id' === $column ) {
		echo $post_id;
	}
}
4

Chức năng

$articles = get_posts( array( 'posts_per_page' => 50 ) );
foreach( $articles as $article ) {
	echo $article->ID;
}
8 đặc biệt tuyệt vời khi bật permalink đẹp

8. Nhận ID bài đăng theo tiêu đề

Có một chức năng WordPress tích hợp và kể từ 3. 0, nó không chỉ hoạt động cho các trang mà còn cho bất kỳ loại bài đăng tùy chỉnh nào. Tham số hàm thứ ba là tên của loại bài đăng (

$articles = get_posts( array( 'posts_per_page' => 50 ) );
foreach( $articles as $article ) {
	echo $article->ID;
}
9 theo mặc định)

/**
 * Add Post ID Column to WordPress Admin
 *
 * @author Misha Rudrastyh
 * @link https://rudrastyh.com/wordpress/get-post-id.html#post_id_column
 */
add_filter( 'manage_posts_columns', 'misha_add_column' );
add_action( 'manage_posts_custom_column', 'misha_column_content', 5, 2 );
// for Pages:
// add_filter( 'manage_pages_columns', 'misha_add_column' );
// add_action( 'manage_pages_custom_column', 'misha_column_content', 5, 2 );
function misha_add_column( $columns ){
	$columns[ 'misha_post_id' ] = 'ID';
	return $columns;
}
function misha_column_content( $column, $post_id ){
	if( 'misha_post_id' === $column ) {
		echo $post_id;
	}
}
7

9. Nhận ID bài đăng bằng sên

Chức năng này tương tự như 

$articles = get_posts( array( 'posts_per_page' => 50, 'fields' => 'ids' ) );
foreach( $articles as $article_id ) {
	echo $article_id;
}
0. Nhưng nếu bài đăng của bạn có cha mẹ (chỉ dành cho loại bài đăng phân cấp), bạn cũng phải chỉ định sên mẹ, ví dụ:
$articles = get_posts( array( 'posts_per_page' => 50, 'fields' => 'ids' ) );
foreach( $articles as $article_id ) {
	echo $article_id;
}
1

/**
 * Add Post ID Column to WordPress Admin
 *
 * @author Misha Rudrastyh
 * @link https://rudrastyh.com/wordpress/get-post-id.html#post_id_column
 */
add_filter( 'manage_posts_columns', 'misha_add_column' );
add_action( 'manage_posts_custom_column', 'misha_column_content', 5, 2 );
// for Pages:
// add_filter( 'manage_pages_columns', 'misha_add_column' );
// add_action( 'manage_pages_custom_column', 'misha_column_content', 5, 2 );
function misha_add_column( $columns ){
	$columns[ 'misha_post_id' ] = 'ID';
	return $columns;
}
function misha_column_content( $column, $post_id ){
	if( 'misha_post_id' === $column ) {
		echo $post_id;
	}
}
0

10. Bằng một cặp khóa và giá trị meta cụ thể

Ok, đôi khi bạn có thể cần nó. Tất nhiên, luôn có thể sử dụng meta_query của

/**
 * Add Post ID Column to WordPress Admin
 *
 * @author Misha Rudrastyh
 * @link https://rudrastyh.com/wordpress/get-post-id.html#post_id_column
 */
add_filter( 'manage_posts_columns', 'misha_add_column' );
add_action( 'manage_posts_custom_column', 'misha_column_content', 5, 2 );
// for Pages:
// add_filter( 'manage_pages_columns', 'misha_add_column' );
// add_action( 'manage_pages_custom_column', 'misha_column_content', 5, 2 );
function misha_add_column( $columns ){
	$columns[ 'misha_post_id' ] = 'ID';
	return $columns;
}
function misha_column_content( $column, $post_id ){
	if( 'misha_post_id' === $column ) {
		echo $post_id;
	}
}
3 cho mục đích đó nhưng hãy để tôi chỉ cho bạn một giải pháp truy vấn SQL

/**
 * Add Post ID Column to WordPress Admin
 *
 * @author Misha Rudrastyh
 * @link https://rudrastyh.com/wordpress/get-post-id.html#post_id_column
 */
add_filter( 'manage_posts_columns', 'misha_add_column' );
add_action( 'manage_posts_custom_column', 'misha_column_content', 5, 2 );
// for Pages:
// add_filter( 'manage_pages_columns', 'misha_add_column' );
// add_action( 'manage_pages_custom_column', 'misha_column_content', 5, 2 );
function misha_add_column( $columns ){
	$columns[ 'misha_post_id' ] = 'ID';
	return $columns;
}
function misha_column_content( $column, $post_id ){
	if( 'misha_post_id' === $column ) {
		echo $post_id;
	}
}
0

# WordPress

Làm cách nào để tìm URL của ID bài đăng WordPress?

Misha Rudrastyh

Chào các bạn và chào mừng đến với trang web của tôi. Trong hơn 10 năm, tôi đã cố gắng hết sức để chia sẻ miễn phí với bạn một số hướng dẫn và mẹo tuyệt vời về WordPress

Tôi có thể tìm thấy URL ở đâu trong WordPress?

Nhấp vào Trang trong điều hướng bên trái, sau đó tìm trang bạn muốn bảo vệ và nhấp vào trang đó. Khi bạn đang ở trong trang, ở phía bên tay phải, hãy cuộn xuống cho đến khi bạn thấy Permalink và nhấp vào đó . Ở đó bạn sẽ thấy URL của trang (được gọi là URL Slug). Nếu bạn đã thêm mật khẩu trang WordPress, bạn phải xóa mật khẩu đó.

ID bài đăng WP là gì?

ID bài đăng là một số duy nhất được gán cho mỗi bài đăng trên trang web WordPress . Mỗi trang cũng chứa một số nhận dạng, được gọi là ID trang. WordPress sử dụng ID để theo dõi mọi phần nội dung trong cơ sở dữ liệu WordPress. Ngoài các bài đăng và trang, còn có ID cho tệp đính kèm phương tiện, danh mục và thẻ.