skip to Main Content

I want to delete some posts that have meta_key = xxx_generate_id programmatically, here is my code

$arg = array(
        'numberposts' => -1,    // The number of posts to retrieve, otherwise 5
        'post_type'   => $publish_as,
        'post_status' => $post_status,
        'meta_key' => 'xxx_generate_id',
    );
$quesryResults = new WP_Query($arg);
    $success = 0;
    $failed = 0;
if ($quesryResults->have_posts()) {
        while ($quesryResults->have_posts()) {
            $quesryResults->the_post();
            if (wp_delete_post(get_the_ID(), true)) {
                $successs++;
            } else {
                $failed++;
            }
        }
        
    }

All posts containing meta_key = xxx_generate_id successfully deleted. But besides successfully deleting the posts it also returned an error like this

WordPress database error: [Unknown column '0' in 'field list']
UPDATE `wp_rank_math_internal_meta` SET `internal_link_count` = '0', `external_link_count` = '0', `0` = '' WHERE `object_id` = 661

WordPress database error: [Unknown column '0' in 'field list']
UPDATE `wp_rank_math_internal_meta` SET `internal_link_count` = '0', `external_link_count` = '0', `0` = '' WHERE `object_id` = 660

WordPress database error: [Unknown column '0' in 'field list']
UPDATE `wp_rank_math_internal_meta` SET `internal_link_count` = '0', `external_link_count` = '0', `0` = '' WHERE `object_id` = 652

It seems like this error is from rank math plugin.

How to solve this error? or to hide it?

2

Answers


  1. Chosen as BEST ANSWER

    I solved this by querying not using WP_Query but using a manual query

    $myposts = $wpdb->get_results($wpdb->prepare("mysql_query_here"));
    

  2. This is the solution to the problem

    On wp-content/plugins/seo-by-rank-math/includes/modules/links/class-links.php line 76
    It should be: $processor->storage->update_link_counts( $post_id, null, $links );
    
    instead of: $processor->storage->update_link_counts( $post_id, 0, $links );
    
    

    Source: https://support.rankmath.com/ticket/error-on-post-delete-and-solution/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search