$args = array(
'post_type' => 'post',
's' => 'Your Post Title',
);
$query = new WP_Query($args);
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
// Display post content or perform other operations
}
} else {
// No posts found
}
wp_reset_postdata();
$post = get_page_by_title('Your Post Title', OBJECT, 'post');
if ($post) {
// Display post content or perform other operations
} else {
// Post not found
}
Метод 3: использование SQL-запроса
Если вы предпочитаете прямой запрос SQL, вы можете использовать глобальную переменную $wpdb для взаимодействия с базой данных WordPress.
global $wpdb;
$title = 'Your Post Title';
$query = $wpdb->prepare(
"SELECT * FROM $wpdb->posts WHERE post_title LIKE %s",
'%' . $wpdb->esc_like($title) . '%'
);
$results = $wpdb->get_results($query);
if ($results) {
foreach ($results as $post) {
// Display post content or perform other operations
}
} else {
// No posts found
}