‘post’, ‘post_status’ => ‘publish’, ‘posts_per_page’ => 5, ‘s’ => sanitize_text_field($tag) ]);
foreach ($results as $post) {
if (!in_array($post->ID, $unique_ids)) {
$posts[] = $post;
$unique_ids[] = $post->ID;
}
}
}
}
// Fallback to focus keyword
if (
empty($posts) &&
!empty($generated['focus_keyword'])
) {
$results = get_posts([
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 5,
's' => sanitize_text_field(
$generated['focus_keyword']
)
]);
foreach ($results as $post) {
if (!in_array($post->ID, $unique_ids)) {
$posts[] = $post;
$unique_ids[] = $post->ID;
}
}
}
// Final fallback
if (empty($posts)) {
$posts = get_posts([
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 5,
'orderby' => 'date',
'order' => 'DESC'
]);
}
return array_slice($posts, 0, 5);
}
function stai_insert_internal_links($article, $generated) { links=stai_g et_i nternal_l inks(generated);
if (empty($links)) {
return $article;
}
$html = "\n\n";
$html .= '
Related Articles
';
$html .= '';
foreach ($links as $post) {
if (empty($post->ID)) {
continue;
}
$html .= sprintf(
'- %s
',
esc_url(get_permalink($post->ID)),
esc_html(get_the_title($post->ID))
);
}
$html .= '
';
return $article . $html;
}
‘post’, ‘post_status’ => ‘publish’, ‘posts_per_page’ => 5, ‘s’ => sanitize_text_field($tag) ]);
foreach ($results as $post) {
if (!in_array($post->ID, $unique_ids)) {
$posts[] = $post;
$unique_ids[] = $post->ID;
}
}
}
}
// Priority 2: Focus Keyword
if (
empty($posts) &&
!empty($generated['focus_keyword'])
) {
$results = get_posts([
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 5,
's' => sanitize_text_field(
$generated['focus_keyword']
)
]);
foreach ($results as $post) {
if (!in_array($post->ID, $unique_ids)) {
$posts[] = $post;
$unique_ids[] = $post->ID;
}
}
}
// Priority 3: Latest Articles
if (empty($posts)) {
$posts = get_posts([
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 5,
'orderby' => 'date',
'order' => 'DESC'
]);
}
return array_slice($posts, 0, 5);
}
/**
• Append Related Articles section
• to generated article content. */ function stai_insert_internal_links( $article, $generated ) {
$links = stai_get_internal_links( $generated );
if (empty($links)) { return $article; }
$html = “”; $html .= ’
Related Articles
‘; $html .=’
’;
foreach ($links as $post) {
if (empty($post->ID)) {
continue;
}
$html .= sprintf(
'%s',
esc_url(
get_permalink($post->ID)
),
esc_html(
get_the_title($post->ID)
)
);
}
$html .= ’
’;
return $article . $html; } ?>