Leave a Reply

3 comments

  1. I think I know what you mean, and if that is the case it’s actually a straightforward operation.

    Firstly get all the object_ids from the term_relationship table.

    $results = $wpdb->get_results("SELECT `object_id` FROM $wpdb->term_relationships WHERE `term_taxonomy_id` = 18");
    

    And then run through each of those values and update_post_meta to 18.

    foreach ( $results as $result )
        update_post_meta($result, '_category_permalink_', '18', true);
    

    It couldn’t be that simple could it?

  2. @deadlyhifi’s answer looks good. I would suggest trying out WP_Query though for a more human readable “select” statement, suppose your term taxonomy 18 is the foo category, here’s the equivalent with WP_Query:

    $query = new WP_Query( array(
        'posts_per_page' => -1,
        'category_name' => 'foo', // slug, not name!
    ) );
    
    while ( $query->have_posts() ) {
        $query->the_post();
        update_post_meta( get_the_ID(), '_category_permalink_', 18, true );
    }
    

    You can also use 'cat' => 18 if you need to stick to the IDs. It’s definitely not faster than the direct SQL method mentioned by @deadlyhifi, but it’s friendlier to the eye.

  3. global $wpdb;
    $query = 'SELECT `object_id` FROM '.$wpdb->term_relationships.' WHERE  `term_taxonomy_id`=18';
    $obj_ids = $wpdb->get_col( $query );
    foreach( $obj_ids as $post_id ) {
        update_post_meta( $post_id, '_category_permalink_', '18' );
    }
    

    Should about do it for you, assuming @kovshenin’s method does not work, since it’s the better method for sure if it does (though I might try a foreach instead of the while personally, either is valid).

    I have not directly tested this, but I’ve tested the query as well as checked the output on $obj_ids, so if it’s not perfect, it’s close.