0

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. I assume that is because text is not stored in database, because it shows only when post renders it self on frontend. How can I include dynamically created text into my code?

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;
} );
2
  • 1
    It's not going to be possible to search for or query posts based on content that is dynamically generated with a shortcode. However are you sure that there isn't metadata on each broadcasted post that could be used to identify them? It seems like your using a plugin, have you asked its developer? Dec 14, 2022 at 8:41
  • Yes, and he said that he does not have time...read this between lines :) Developer does not have time, but I should figure out how to work with this. By his words - there is no custom meta tags :) There is only one table in database which is related to this plugin, 3wp_broadcast_broadcastdata, but there are relations between parent-child blogs
    – ZeusDev
    Dec 14, 2022 at 15:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.