wp_back/wp-content/themes/simple-theme/api-posts/api-posts.php

103 lines
3.4 KiB
PHP
Raw Permalink Normal View History

2024-05-27 15:01:43 +03:00
<?php
add_action('rest_api_init', 'endlessPosts');
function endlessPosts()
{
register_rest_route('api-posts/v1', "/endlessPosts", array(
'methods' => WP_REST_Server::READABLE,
'callback' => 'getEndlessPostsResults'
));
}
function getEndlessPostsResults($data)
2024-05-27 15:01:43 +03:00
{
global $wpdb;
$link = $data['permalink'];
$paged = $data['paged'];
if ($paged < 0){
$paged = 1;
}
$result = [];
2024-05-29 16:29:52 +03:00
$tag_slug = url_to_postid($link); //Получаем айди поста по пермалинку
$tags = get_the_tags($tag_slug); //Получаем тэги по айди
$i = 0;
$postSlugs = [];
2024-05-30 17:15:17 +03:00
if (!$tags){
$tags = 'null';
}
if (is_array($tags) && count($tags) > 0) {
$postSlugs = array_column($tags, 'term_id');
} else {
// Handle the case when no tags are found
$postSlugs = array(); // or some other default value
2024-05-29 16:29:52 +03:00
}
2024-05-30 17:15:17 +03:00
2024-05-27 15:01:43 +03:00
$id_from_permalink = explode('/', $link);
$id_from_permalink = explode('-', $id_from_permalink[3]);
$id = $id_from_permalink[0];
2024-05-30 17:15:17 +03:00
$requestedPostFromPermalink = get_post($id);
$requestedPost = [
'id' => $requestedPostFromPermalink->ID,
"post_author" => $requestedPostFromPermalink->post_author,
"post_date" => $requestedPostFromPermalink->post_date,
"post_date_gmt" => $requestedPostFromPermalink->post_date_gmt,
"post_content" => $requestedPostFromPermalink->post_content,
"post_title" => $requestedPostFromPermalink->post_title,
"post_excerpt" => $requestedPostFromPermalink->post_excerpt,
"post_status" => $requestedPostFromPermalink->post_status,
"comment_status" => $requestedPostFromPermalink->comment_status,
"ping_status" => $requestedPostFromPermalink->ping_status,
"post_password" => $requestedPostFromPermalink->post_password,
"post_name" => $requestedPostFromPermalink->post_name,
"to_ping" => $requestedPostFromPermalink->to_ping,
"pinged" => $requestedPostFromPermalink->pinged,
"post_modified" => $requestedPostFromPermalink->post_modified,
"post_modified_gmt" => $requestedPostFromPermalink->post_modified_gmt,
"post_content_filtered" => $requestedPostFromPermalink->post_content_filtered,
"post_parent" => $requestedPostFromPermalink->post_parent,
"guid" => $requestedPostFromPermalink->guid,
"menu_order" => $requestedPostFromPermalink->menu_order,
"post_type" => $requestedPostFromPermalink->post_type,
"post_mime_type" => $requestedPostFromPermalink->post_mime_type,
"comment_count" => $requestedPostFromPermalink->comment_count,
"filter" => $requestedPostFromPermalink->filter,
];
$result[] = ['requested-post' => $requestedPost];
2024-05-27 15:01:43 +03:00
2024-05-30 17:15:17 +03:00
2024-05-29 16:29:52 +03:00
$args = array( // Создаем массив с параметрами запроса
'tag__and' => $postSlugs,
2024-05-27 15:01:43 +03:00
'posts_per_page' => 10,
'paged' =>$paged,
'orderby'=>'date',
'order'=> 'DESC'
2024-05-27 15:01:43 +03:00
);
2024-05-29 16:29:52 +03:00
$posts = new WP_Query($args); // Инициализация запроса
2024-05-27 15:01:43 +03:00
while ( $posts->have_posts() ) {
$posts->the_post();
$result[] = array(
2024-05-30 17:15:17 +03:00
'id' => get_the_ID(),
2024-05-27 15:01:43 +03:00
'permalink' => get_the_permalink(),
);
}
return $result;
}
//"http://pamtest.ru/702-oformlenie-osago-na-a-b-c-d-otkryto-vsem-agenta/"
//"http://pamtest.ru/294-6-j-urok-kak-strahovomu-agentu-zadavat-voprosy-i-rabotat-s-vozrazheniyami/"