php – WordPress Built-In search function not working properly

Question

I need to move certain articles on my website, all of them, to another unique category. I am using specific phrase which is at bottom of every article, but after code execution, only two articles are moved to new category, not all of them. Also when I search through all posts using native Dashboard-All posts option, it returns only those two articles, not all of them. Any idea?

Of course, Indexing is enabled, have Yoast SEO installed, sitemap is regular, updated, but search does not returns correct results.

My code, just for reference:

<?php
// Move to category Breaking News
$target_category_id = 1982;

// Get all posts that contain the search phrase from Syndication
$posts = get_posts(array(
  's' => 'and has been published here with permission',
  'post_status' => 'any',
  'posts_per_page' => -1 // This will retrieve all posts
));

// Loop through the posts and move them to the target category
foreach ($posts as $post) {
  // Remove the post from any existing categories
  $current_categories = wp_get_post_categories($post->ID);
  wp_remove_object_terms($post->ID, $current_categories, 'category');

  // Add the post to the target category
  wp_set_post_categories($post->ID, array($target_category_id), true);
}

EDIT That specific phrase is created dynamically, because I am using WP Broadcast plugin to add text at the bottom of every broadcasted article, but I am not sure is that phrase stored in database or that text is dynamically populated (created) every time post is rendered on frontend.

This is the code I am using to generate message (phrase) at the bottom of every article:

<?php
function broadcasted_from()
{
    // Check that Broadcast is enabled.
    if ( ! function_exists( 'ThreeWP_Broadcast' ) )
        return;
    // Load the broadcast data for this post.
    global $post;
    $broadcast_data = ThreeWP_Broadcast()->get_post_broadcast_data( get_current_blog_id(), $post->ID );
    // This post must be a child. Check for a parent.
    $parent = $broadcast_data->get_linked_parent();
    if ( ! $parent )
        return;

    // Fetch the permalink
    switch_to_blog( $parent[ 'blog_id' ] );
    $blog_name = get_bloginfo( 'name' );
    $permalink = get_post_permalink( $parent[ 'post_id' ] );
    restore_current_blog();

    // And now assemble a text.
    $r = sprintf( 'This article appeared in <a href="%s">%s</a> and has been published here with permission.', $permalink, $blog_name );
    return $r;
}
add_shortcode( 'broadcasted_from', 'broadcasted_from' );

add_filter( 'the_content', function( $content )
{
// Get the broadcasted from text.
$sc_text = do_shortcode( '[broadcasted_from]' );
// Add the text to the content.
$content .= $sc_text;
// Return the expanded content.
return $content;
} );

0
ZeusDev 2 months 2022-12-13T12:47:01-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse