Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a site with about 24,000 posts and 200 posts daily and the database will get huge soon. So I plan to transfer the old posts to custom tables to reduce the database load. There's no need to show these posts on the site. I will keep it for search engines and avoiding sql_calc_found_rows problems. These are the three tables I have created:

wp_posts -> wp_custom_posts

wp_postmeta -> wp_custom_postmeta

wp_term_relationships -> wp_custom_term_relationships

I have transferred old posts to these tables manually using phpMyAdmin and use a function to make WordPress use my custom post table instead of wp_posts if the post does not exists in it:

function custom_prefix_change($sql) {
    global $wpdb,$wp;
    $prefix = $wpdb->prefix;
    if(is_single()){
        //check if the post exists in the genuine posts table
        $results = $wpdb->get_results($sql,"ARRAY_A");

        if($results[0]['ID']){
            //if exists then return
            return $sql;
        }else{
            //else change the query
            $sql = str_replace("$wpdb->posts","{$wpdb->prefix}custom_posts",$sql);          
        }
    }
    //echo $sql;
    return $sql;
}
add_action('posts_request',"custom_prefix_change");

However, my old posts in this case show up with no meta and taxonomies values. How can I do the same with the meta and relationships tables? Any suggestions?

share|improve this question
    
The problem is that the meta and term relation needs the post ID to remain the same as it was on the "old posts" table. –  Bainternet Dec 21 '12 at 16:33
    
all posts have the same id and structure as the original –  user1921704 Dec 21 '12 at 19:58
    
i see will i added an answer –  Bainternet Dec 22 '12 at 18:33

1 Answer 1

a simple and Not perfect solution which will do most of the job for you is to overwrite the table names like this:

function custom_prefix_change($sql) {
  global $wpdb,$wp;
  $prefix = $wpdb->prefix;
  if(is_single()){
      //check if the post exists in the genuine posts table
      $results = $wpdb->get_results($sql,"ARRAY_A");

      if($results[0]['ID']){
          //if exists then return
          return $sql;
      }else{
          //else change the query
          $sql = str_replace("$wpdb->posts","{$wpdb->prefix}custom_posts",$sql);
          $wpdb->term_relationships = $wpdb->prefix . 'custom_term_relationships';
          $wpdb->postmeta = $wpdb->prefix . 'custom_postmeta';
      }
  }
  return $sql;
}

Now for a complete solution you will need to replace the tables in the terms_clauses filter hook for get_terms and get the meta data from the custom table yourself using the get_post_metadata for get_post_meta.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.