从自定义帖子标题 WordPress 回显自定义帖子类型 ID


echo custom post type ID from custom post title WordPress

如果搜索了几个小时:-(希望有人能帮助我。

我属于一个名为"grhdogs"的自定义帖子类型。在那里,我需要获取该页面的当前页面标题,并在另一种名为"slideshow"的自定义帖子类型中使用该标题进行搜索,并在"slideshow"中回显(作为字符串)匹配页面标题的页面 ID。

背景是,在两种自定义帖子类型中始终匹配页面标题。我必须将它们放在一起,因为"grhdogs"是一个帖子,而"幻灯片"是我想在帖子中显示的匹配幻灯片。

你可以尝试这样的事情。

<?php
//get the current title of the page
$postTitle = get_the_title(); 
//search for similer post in db table
$postID = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE   post_type='slideshow' AND post_title = %s",$postTitle));
//print id
 echo $postID;  
//or use get_post($post_id) to get whatever you want
$getCustomPost = get_post($postID);
echo $getCustomPost->id;
?>