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

81 lines
2.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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)
{
global $wpdb;
$link = $data['permalink'];
$paged = $data['paged'];
if ($paged < 0){
$paged = 1;
}
$result = [];
$tag_slug = url_to_postid($link); //Получаем айди поста по пермалинку
$tags = get_the_tags($tag_slug); //Получаем тэги по айди
$i = 0;
$postSlugs = [];
foreach ($tags as $key => $slug){ //Создаём массив с ID тэгов
$postSlugs[] += $tags[$i]->term_id;
$i++;
}
$id_from_permalink = explode('/', $link);
$id_from_permalink = explode('-', $id_from_permalink[3]);
$id = $id_from_permalink[0];
$requestedPost = get_post($id);
$result[] = ['requested-post' => $requestedPost];
$args = array( // Создаем массив с параметрами запроса
'tag__and' => $postSlugs,
'posts_per_page' => 10,
'paged' =>$paged,
'orderby'=>'date',
'order'=> 'DESC'
);
$posts = new WP_Query($args); // Инициализация запроса
while ( $posts->have_posts() ) {
$posts->the_post();
$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++;
}
$actualTag = get_the_tags();
$terms = get_the_category();
$result[] = array(
'post_id' => get_the_ID(),
'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/"