В настоящее время я использую фильтр the_posts и проверяю WP_Query->is_main_query( ). Также я попробовал $query->in_the_loop(), но он всегда возвращает false. В результате возникает проблема: the_posts выполняется и для других запросов (например, перечисления сообщений на боковых панелях, в виджетах и т. д.). В результате я либо получаю список, включающий только внешние статьи, либо комбинированный список с внутренними и внешними публикациями по желанию, но верхний, нижний колонтитул и боковая панель на странице исчезают.
Код:
modify_query_for_front_page:
Код: Выделить всё
public function modify_query_for_front_page($query)
{
if ($query->is_main_query() && $query->is_front_page()) {
global $combined_articles;
// Set the query to load no default posts
$query->set('posts_per_page', 0);
if (empty($combined_articles)) {
$mergedContent = new MergedOverviewPageContent($this->connector);
// collect all posts from internal db and merge with external articles
$combined_articles = $mergedContent->getCombinedPostData();
}
}
}
Код: Выделить всё
add_action('pre_get_posts', [$this, 'modify_query_for_front_page']);
add_filter('the_posts', function($posts, \WP_Query $query) {
global $combined_articles;
if ($query->is_main_query() && is_front_page() && !empty($combined_articles)) {
$combined_posts = array_map(
fn($article) => create_virtual_wp_post($article),
$combined_articles
);
return $combined_posts;
}
return $posts;
}, 10, 2);
Код: Выделить всё
public static function create_virtual_wp_post($article) {
// Create a new WP_Post object for each article
$post = new \stdClass(); // Create a new stdClass to hold post data
// Assign unique data from the article to the new post
$post->ID = 0; // Virtual post, no real ID
$post->post_author = 1; // Default author or use the article's author if available
$post->post_date = isset($article['date']) ? $article['date'] : current_time('mysql');
$post->post_date_gmt = isset($article['date']) ? get_gmt_from_date($article['date']) : current_time('mysql', 1);
$post->post_content = isset($article['content']) ? $article['content'] : '';
$post->post_title = isset($article['title']) ? $article['title'] : 'Untitled';
$post->post_excerpt = isset($article['summary']) ? $article['summary'] : '';
$post->post_status = 'publish';
$post->comment_status = 'closed';
$post->ping_status = 'closed';
$post->post_password = '';
$post->post_name = sanitize_title($post->post_title); // Create a sanitized post slug
$post->to_ping = '';
$post->pinged = '';
$post->post_modified = '0000-00-00 00:00:00'; // No modification time for virtual posts
$post->post_modified_gmt = '0000-00-00 00:00:00';
$post->post_content_filtered = '';
$post->post_parent = 0;
$post->guid = isset($article['link']) ? $article['link'] : home_url(); // Use article link or home URL
$post->menu_order = 0;
$post->post_type = 'post'; // Assuming this is a post, not a page
$post->post_mime_type = '';
$post->comment_count = 0;
$post->filter = 'raw';
// Mark the post as virtual to differentiate it from real posts
$post->is_virtual = true;
// Return the newly created WP_Post object
return new \WP_Post($post); // Convert stdClass to WP_Post object
}
Подробнее здесь: https://stackoverflow.com/questions/790 ... -structure
Мобильная версия