• Hello,

    Is there anyone know how to change wordpress table name and still make wordpress working?

    I have searched Google but can’t find any hint.

    It’s for a project.

    Thank you

Viewing 6 replies - 1 through 6 (of 6 total)
  • Why do you want to change the table names?

    Do you want to change all of them?

    … and you’re not looking for a way to change the table prefix wp_ ?

    I guess you’re looking for a way to use for example:

    wp_my_posts

    instead of

    wp_posts

    Here’s an untested idea:

    /**
     * Change the posts table name from 'wp_posts' to 'wp_my_posts'
     */
    add_action( 'registered_taxonomy', function(){
        global $wpdb;
        $wpdb->posts = 'my_posts';
    });

    where registered_taxonomy is just a hook that’s fired very early.

    We might have to find a more suitable hook.

    Thread Starter ekajuan

    (@ekajuan)

    Hai Birgire,

    It’s not the prefix that I want to change.
    The reason why I want to change the name of the table is due to a project that requires a relevant table name.
    It has something to do with woocommerce. It stores products in wp_post.
    I want it to be stored in let’s say…in wp_products.
    I’m thinking about creating a new table with that kind of name and the same structure or just change the name of the existing wp_post.

    Do I have to really change the table name? Yes.

    You can try:

    /**
     * Change the posts table name from 'wp_posts' to 'wp_my_posts'
     * Version #2 - The 'muplugins_loaded' action
     */
    add_action( 'muplugins_loaded', function(){
        global $wpdb;
        $wpdb->posts = 'my_posts';
    });

    where muplugins_loaded is the earliest action hook available.

    You can also hijack the query filter, that’s fired before the muplugins_loaded action:

    /**
      * Change the posts table name from 'wp_posts' to 'wp_my_posts'
      * Version #3 - Hijack the 'query' filter, the first time it's fired.
      */
    
    add_filter( 'query', 'b2e_change_table_name' );
    
    function b2e_change_table_name( $query ){
        remove_filter( current_filter(), __FUNCTION__ );
        global $wpdb;
        $wpdb->posts = 'my_posts';
        return $query;
    }

    It might also be worth trying this in the global scope:

    /**
      * Change the posts table name from 'wp_posts' to 'wp_my_posts'
      * Version #4 - The global scope
      */
    global $wpdb;
    $wpdb->posts = 'my_posts';

    before any action/filters are fired.

    Does this work for you?

    Thread Starter ekajuan

    (@ekajuan)

    Well, I have tested the three of them, by putting them in functions.php and then change ‘my_posts’ according to my table name without the prefix.
    But they all still don’t work.

    The WP still can’t read the name altered table.

    Thread Starter ekajuan

    (@ekajuan)

    Wait a minute, I have to include the table prefix in order to make your suggested codes working.

    $wpdb->posts = 'wp_my_posts';

    well, in my case I change wp_my_posts according to my table’s name.

    Code No.1 doesn’t work though.
    But code No.2 and 3, they work!!

    To be honest I can’t get my head around code no.1 and no.2, even though I can see the big picture.

    No.3 certainly the simplest way to do it.

    Thank you, Birgire…

    @ekajuan Thanks for reporting back, glad to hear it worked for you ??

    Hopefully this will be useful to others in the same situation.

    You should then consider marking this thread as resolved.

    Best regards

    ps: probably the hook is fired to late in #1.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Changing table name and make wordpress still working’ is closed to new replies.