Personal WP Customization Notes (PWCN)

Records of customizations actually made in WordPress, notes when dealing with problems that occurred, customization of block themes, etc.

The method of obtaining information for individual posts using REST API may be different depending on the permalink settings (note)

I am not giving a complete interpretation, but I am writing this as a memo because it seems like it when I actually tried it. .. .

When obtaining information on individual posts via REST API, the endpoints differed as follows.

[If the permalink is ID (%post_id%)]

Home URL/wp-json/wp/v2/Post type (posts for posts)/ID

[If the permalink is other (character string)]

Home URL/wp-json/wp/v2/Post type (posts for posts)?slug=〇〇

And I found out that if the permalink is an ID, you have to use wp_remote_get(), and in other cases, you have to use file_get_contents() to get the information.

It took me quite a while to figure this out, but is this a WordPress specification or a bug? .. I’m not sure.

By the way, the following is the code that was tested and completed. It roughly divides the permalink depending on whether it is a number or not, gets the ID of the eye-catching image, and outputs the value returned by the shortcode.

There are also differences in the variables when extracting with wp_remote_get() and file_get_contents(), so you may need to be careful.

function pwcn_rest_api_post_data_callback(){
/* Sample when the slug in a post is the post ID (%post_id) */
    //$home_url = '〇〇'; // Site URL
    //$post_type = 'posts'; // Post type
    //$slug = '△△'; // slug

/* Sample for character slug in post */
	$home_url = '〇〇';//Do not include the trailing "/"
	$post_type = 'posts';//Posts are "posts"
	$slug = '□□';//Do not include "/" before and after

/* Sample for custom post type + string slug */
	//$home_url = '〇〇';//Do not include the trailing
	//$post_type = '◇◇';//Posts are "posts"
	//$slug = '□□';//Do not include "/" before and after

//To prevent incorrect input, remove the "/" at the beginning of $slug and the end of $home_url.
    $slug = ltrim($slug, '/');
    $home_url = rtrim($home_url, '/');

if (ctype_digit($slug)) {// If the slug is only numbers
	$post_url = $home_url . '/wp-json/wp/v2/' . $post_type . '/' . $slug;
	$response = wp_remote_get($post_url); // Get data from REST API using wp_remote_get

	$body = wp_remote_retrieve_body($response); // Get response body
	$data = json_decode($body, true); // Decode JSON data

	if (isset($data['featured_media'])) {
		$image_id = $data['featured_media'];
	} else {
		$image_id =  'No images were found.';
	}

} else {//If the slug is not just numbers
	$post_url = $home_url . '/wp-json/wp/v2/' . $post_type . '?slug=' . $slug;
	
	//Get json data
		$json_data = file_get_contents($post_url);
		$decoded_data = json_decode($json_data, true);

// Get the ID of the featured image
	if (isset($decoded_data[0]['featured_media'])) {
		$image_id = $decoded_data[0]['featured_media'];
	}else{
		$image_id = 'Image not found';
	}
}
	return $image_id;
}

function view_data(){
    $image_id = pwcn_rest_api_post_data_callback();
        return $image_id;
}
add_shortcode('test-post-view','view_data');

If you actually have a site with an ID permalink structure and another site, you might be able to say “I see” if you try it.

Please note that this page is a verification result as of WordPress 6.5, and the behavior may be different in other versions, and the environment may be affected. I would be happy if I could share my opinion with someone who has a personal solution.

but. .. If this were the specification, it would be quite confusing. .. .. .