Enable Custom Post Order via code in WordPress

Recently I enabled the great Simple Custom Post Order (SCPO) plugin in a bunch of sites. After you activate it you can go into its settings and enable sorting for all posts, custom post types, taxonomies, etc.

We had a particular case though. We manage a network of nearly 200 WordPress sites. It makes no sense logging to 200 WP sites and update their values. In every site, we needed to

  1. Upload and activate SCPO.
  2. Enable sorting for categories only.

First one is handled by ManageWP, so that’s done with a few clicks. The second seemed a bit more tricky, since there’s not much documentation, since the plugin works great out of the box.

I asked a question in the forum and after that and a bit of digging into the code and database, I found a solution:

SCPO options are stored in wp_optionstable, under scporder_options name. In other words, you can manipulate it using the update_option filter. Or, as @mplusb mentioned:

The option’s name where they are stored is: scporder_options
which is an array and in ‘objects’ will keep all the custom posts and in ‘tags’ the taxonomies.

Answer from @mplusb

That means a simple solution is:

update_option('scporder_options', array('objects' => '', 'tags' => array('category')));

I also added a check to ensure the plugin is active and a second one to make sure we are not overwriting options if scporder_options is set, so the final code is:

if (is_plugin_active('simple-custom-post-order/simple-custom-post-order.php')) {
    if (!get_option('scporder_options')) {
        update_option('scporder_options', array('objects' => '', 'tags' => array('category')));
    }
}

Leave a Reply