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

88 lines
2.4 KiB
PHP
Raw 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, $paged = 1)
{
global $wpdb;
2024-05-29 16:29:52 +03:00
2024-05-27 15:01:43 +03:00
$link = $data['permalink'];
2024-05-29 16:29:52 +03:00
$tag_slug = url_to_postid($link); //Получаем айди поста по пермалинку
// var_dump($tag_slug);
$tags = get_the_tags($tag_slug); //Получаем тэги по айди
// var_dump($tags[0]->term_id);
// die();
$i = 0;
$postSlugs = [];
foreach ($tags as $key => $slug){ //Создаём массив с ID тэгов
$postSlugs[] += $tags[$i]->term_id;
// if ($key < count($tags) - 1) {
// $postSlugs .= ', ';
// }
$i++;
}
// $tag_name = $tag->name;
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-29 16:29:52 +03:00
$post_id = $id;
// $category = get_the_tags($post_id);
2024-05-27 15:01:43 +03:00
$result = array();
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,
2024-05-29 16:29:52 +03:00
'paged' => $paged,
'orderby'=>'date'
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();
2024-05-29 16:29:52 +03:00
$tags = get_the_tags();
$postsTags ='';
$tags = get_the_tags();
foreach ($tags as $key => $tag){
$postsTags .= $tags[$i]->name;
if ($key < count($tags) - 1) {
$postsTags .= ', ';
}
$i++;
}
2024-05-27 15:01:43 +03:00
2024-05-29 16:29:52 +03:00
$actualTag = get_the_tags();
$terms = get_the_category();
2024-05-27 15:01:43 +03:00
$result[] = array(
'permalink' => get_the_permalink(),
'terms' => $terms[0]->slug,
2024-05-29 16:29:52 +03:00
'title' => get_the_title(),
'author' => get_the_author(),
'date' => get_the_date(),
'post-body' =>get_the_content(),
'tags' => get_the_tags()[0]->name
2024-05-27 15:01:43 +03:00
);
}
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/"