September 4, 2019

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. At my current job, we manage a network of nearly 200 WordPress sites. It makes no sense logging to 200 sites’ admin to update their values. In every site, we needed to

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

First one can be handled by ManageWP or other remote-access tools. The second seemed a bit more tricky. There’s not much documentation, since the plugin works great out of the box.

I asked this 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_options table, 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')));
    }
}