From 830c66689b0425be653978a67eb0bf3b87bdca11 Mon Sep 17 00:00:00 2001 From: Kr1PtunZ Date: Thu, 30 May 2024 19:15:17 +0500 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=B0=D0=B2=D0=BA=D0=B8=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../simple-theme/api-posts/api-posts.php | 62 +++++++++++++------ 1 file changed, 42 insertions(+), 20 deletions(-) diff --git a/wp-content/themes/simple-theme/api-posts/api-posts.php b/wp-content/themes/simple-theme/api-posts/api-posts.php index 78ae902..7425e70 100644 --- a/wp-content/themes/simple-theme/api-posts/api-posts.php +++ b/wp-content/themes/simple-theme/api-posts/api-posts.php @@ -27,20 +27,57 @@ function getEndlessPostsResults($data) $tags = get_the_tags($tag_slug); //Получаем тэги по айди $i = 0; $postSlugs = []; - foreach ($tags as $key => $slug){ //Создаём массив с ID тэгов - $postSlugs[] += $tags[$i]->term_id; - $i++; + + 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 + } + $id_from_permalink = explode('/', $link); $id_from_permalink = explode('-', $id_from_permalink[3]); $id = $id_from_permalink[0]; - $requestedPost = get_post($id); + $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]; + + $args = array( // Создаем массив с параметрами запроса 'tag__and' => $postSlugs, 'posts_per_page' => 10, @@ -52,23 +89,8 @@ function getEndlessPostsResults($data) $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(), + 'id' => get_the_ID(), 'permalink' => get_the_permalink(), ); } -- 2.34.1