2

We have an in-house WordPress plugin that creates content using a custom-post-type. Essentially, the content created is, for all intents and purposes, identical to WordPress 'Pages'.

The idea is that we add these custom 'pages' - via our plugin - and then deploy/update them for whomever is using the plugin. We've used our plugin to create a series of these pages, but the question is how to port this custom content with the plugin.

Initially we were thinking that since our posts have a custom post-type, we can easily identify them in the DB. In fact, we use that to remove our custom content when uninstalling the plugin.

But how do we do the inverse? Ideally we'd like to update the plugin, and with it, any custom content that we've added or modified.

Originally we were thinking of just using MySQL scripts to add this custom post content, but after a bit of research, this doesn't seem like the right way to do it.

I know WordPress has a wp_insert_post() function, but I'm just not sure of how it all fits together.

Ideally, the answer would be an overview of the process for updating our custom content. For instance, should there be a function in our plugin that, upon installation, looks for a sql file and creates new posts from it?

Thanks!

3
  • Haven't tried anything yet - just looking for direction as to what to try.
    – jgohil
    Sep 29, 2016 at 14:43
  • This is what I'm leaning toward, but I don't quite understand what happens when updating an existing post. Will that method of programmatically adding posts also work for updating existing posts?
    – jgohil
    Sep 29, 2016 at 14:48
  • this is probably a good starting point, along with the wp_insert_post() function
    – andrew
    Sep 29, 2016 at 14:58

1 Answer 1

1

If I understand this correctly and you are trying to keep content sync'ed across multiple installations via plugin updates you will need to create a unique ID for all of your content. When you do updates programatically you are updating by the post ID but each installation will have different post IDs. You could use a post meta field to give each post a unique ID specific to your plugin. You can then query the posts based on your unique meta field.

I would code the plugin so it checks to see if there is a post with your unique ID. If it is not found then you insert a new post. If it is found then you update the post.

Let's say you have created your first piece of content and it's unique id is going to be jgohil_1. Your plugin might use something like the following to check for that unique id and then update or insert depending on if it exists.

<?php

// our meta key/value pair for our unique id
$meta_key = 'jgohil_unique_id';
$meta_value = 'jgohil_1';

// set our new content
$new_content = 'The new content here';
$new_title = 'The new title here';

// check the database to see if we have created the post already by querying for our unique id
global $wpdb;

$sql = $wpdb->prepare (
    "SELECT post_id 
    FROM $wpdb->postmeta 
    WHERE meta_key = %s 
    AND meta_value = %s",
    $meta_key,
    $meta_value
);

$post_id = $wpdb->get_var( $sql );

// if we got a post id then update else insert
if ( $post_id ) {

    // set up the data for updating
    $data = array(
        'ID' => $post_id,
        'post_content' => $new_content,
        'post_title' => $new_title,
    );

    // update the post
    $updated = wp_update_post( $data );

    if ( is_wp_error( $updated ) ) {
        // do some error handling here
    }

} else {
    $data = array(
        'post_title' => $new_title,
        'post_content' => $new_content
        'post_type' => 'your_custom_post_type'
    );

    $post_id = wp_insert_post( $data );

    // if the insert worked give the post the unique meta id
    if ( ! is_wp_error( $post_id ) && $post_id !== 0 ) {
        update_post_meta( $post_id, $meta_key, $meta_value );
    } else {
        // do some error handling here
    }
}

If you need to handle multiple pieces of content at once you would want to use a loop and change the meta value each time.

2
  • That's exactly what we're trying to do! In our case, we are the only ones adding content to our custom post types - so we were planning to use the post title, paired with the custom post_type to identify our posts (regardless of ID) - does that seem like it would work based on your suggestion? (in lieu of another post meta)
    – jgohil
    Sep 29, 2016 at 15:57
  • As long as your custom post type name is fairly unique you likely wouldn't run into any collisions. The only problem I can think of is that if you start changing post titles it might make it difficult to maintain when users are hopping from different plugin versions. Users could also potentially change the title of the post and you could run into issues there as well.
    – Jared
    Sep 29, 2016 at 16:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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