CMB2 Pipes adds Posts 2 Posts support

Posts 2 Posts first implementation in CMB2 Pipes.

The starting point: why?

The Custom Meta Boxes 2 plugins offers, via filters, the possibility to intercept the meta field input or redirect their output to other relevant post (or user, or option) fields.
Under the uniform and clean interface Custom Meta Boxes 2 so easily builds can then be hidden not just fields writing to a post or user meta fields but functions storing those values in some other ways.
WordPress so easily allows adding meta boxes to the admin screens that sometimes it’s just too dispersed and while I can find my way around some clients of mine will wander a little before setting on the right UI control.
I’ve also grown fond of the Adminimize plugin which makes a wonderful job at UI removal and simplification and in tandem with Custom Meta Boxes 2 will allow for simple, clean and spot-on UIs.

I’m lazy

I do not want to write the same code redirecting the meta value reading or writing over and over again and wanted a go-to solution I could iterate and rely upon: here it comes CMB2 Pipes.
While still in its early stages it already allows me to do something I crave: one to one front-end to back-end correspondence; in the case below I’ve cleaned the interface to the point where only fields relevant to the theme the client will use and see remain. The expectation is to see, in this order:

  • the featured image
  • the quote (the post title)
  • a link to the quote author (a related post)

The code below, along with a radical use of Adminimize and some hooks, will allow me to build a UI hiding WordPress inner workings from the client (which usually does not care).


add_action( 'cmb2_init', 'post_meta_boxes' );
    function post_meta_boxes() {

        $post_meta_box = new_cmb2_box( array(
            'id'           => 'post-metabox',
            'title'        => __( 'A Meta Box', 'cmb2' ),
            // a custom post type
            'object_types' => array( 'quote' )
        ) );

        $post_meta_box->add_field( array(
            'name' => __( 'A representative image', 'cmb2' ),
            // a `_thumbnail_id` meta will be saved...
            'id'   => '_thumbnail',
            'type' => 'file',
            'options' => ['url'=> false]
        ) );

        $post_meta_box->add_field( array(
            'name' => __( 'The quote', 'cmb2' ),
            // no meta called `quote` will be written, not really relevant
            'id'   => cmb2_pipe('quote','<>', 'post_title'),
            'type' => 'text',
        ) );

        $post_meta_box->add_field( array(
            'name' => __( 'Quoted author', 'cmb2' ),
            // once again not relevant, no meta called `quote_author` will be saved
            'id'   => cmb2_p2p_pipe('quote_author','<>', 'author_to_quote', 'to'),
            'type' => 'select',
            'options' => Authors::get_list('ID','post_title')
        ) );
    }

the benefit is no additional code to redirect the meta fields in and output has been written. CMB2 Pipes clean UI

Next

I’ve covered the current code with tests and have some confidence to move on to filtering in and outputs with an interface I still have to think about but has to happen .