Title: SQL script for publishing
Last modified: August 31, 2016

---

# SQL script for publishing

 *  [D C](https://wordpress.org/support/users/derekryanclarke/)
 * (@derekryanclarke)
 * [10 years, 4 months ago](https://wordpress.org/support/topic/sql-script-for-publishing/)
 * So I am running into some issues here with auto publishing. I am mimic the process
   that accure once the Publish button is pressed with an SQL script.
 * A bit of backstory:
    I have a site that it is feed posts from feedly. By default
   these posts are put into draft mode. Using the following script, I have figured
   out a way to change the post from draft to publish. UPDATE DB_Name.wp_posts SET
   post_status = replace(post_status, ‘draft’, ‘publish’), post_name=id where post_status
   = ‘draft’;
 * This works great and does switch from draft to publish and does assign a slug.
   However, when a post comes from feedly, it does not create a featured image. 
   To do this, I have installed a plugin (Quick Featured Images Pro). The plugin
   basically scans the post and uses the first image it sees as the featured image.
 * Here is the catch. The plugin only works if you the feedly post is set to draft
   first and then you manually login and click publish. Thus, my script only makes
   the chagnes in the database, but does not fully do what I need.
 * So my question would be, does anyone know how to script (php or sql) the process
   of pushing the publish botton? Or better yet, how to run the plugin script after
   my script above is run?
 * Thanks in advance.

Viewing 10 replies - 1 through 10 (of 10 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [10 years, 4 months ago](https://wordpress.org/support/topic/sql-script-for-publishing/#post-6940439)
 * The plugin almost certainly works off of a filter or action hook that fires when
   the post is published. There’s a slew of potential hooks that might be used, 
   so you need to examine the plugin code to determine which hooks it’s using. Once
   you find that, your code needs to apply or do the same filter or action. You 
   may need to pass the same values as the `apply_filters()` or `do_action()` call
   does in the core source code. It depends on what values the plugin uses. Even
   if no values are used (unlikely), you still need to pass dummy parameters to 
   match the number passed in core source.
 *  Thread Starter [D C](https://wordpress.org/support/users/derekryanclarke/)
 * (@derekryanclarke)
 * [10 years, 4 months ago](https://wordpress.org/support/topic/sql-script-for-publishing/#post-6940624)
 * Thanks for the response. Any idea how to do this with a cron rather than running
   it via a SQL script?
 * Or better yet, any idea how to duplicate the action of clicking the “publish”
   button?
 * I think you are dead on with the plugin working from a hook or filter, but digging
   through the plugin code is pretty daunting.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [10 years, 4 months ago](https://wordpress.org/support/topic/sql-script-for-publishing/#post-6940629)
 * There’s some issues using cron with WP, I’d look at using [wp_schedule_event()](http://codex.wordpress.org/Function_Reference/wp_schedule_event)
   before considering cron. It too has some issues, but nothing serious IMO.
 * You shouldn’t have to dig too deep into the plugin code, you merely need to find
   the right call to `add_action()` or `add_filter()`. I would bet the action is
   likely ‘draft_to_publish’ or something closely related. Are you familiar with
   the *nix `grep` command? You can quickly identify all files that contain a certain
   text string with it. They are all likely in one file. If you don’t have terminal
   access, unzip the plugin files on your own computer and use something equivalent
   that runs on your O/S.
 *  Thread Starter [D C](https://wordpress.org/support/users/derekryanclarke/)
 * (@derekryanclarke)
 * [10 years, 4 months ago](https://wordpress.org/support/topic/sql-script-for-publishing/#post-6940653)
 * Yeah I scanned it, but there are dozens of instances of `add_action()` and `add_filter()`.
 * I looked to `wp_schedule_event()` and this may work, but still not sure how to
   configure it to set all draft posts to publish.
 * I am not new to WP, but definitely new the the `wp_cron` side of things.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [10 years, 4 months ago](https://wordpress.org/support/topic/sql-script-for-publishing/#post-6940654)
 * Basically just follow the example. Where the example has `// do something` you
   could get all draft posts created in the last time period. For each post object
   change `$post->post_status` to ‘publish’, then use `wp_update_post()`. Doing 
   so will cause ‘draft_to_publish’ and all other appropriate actions to fire.
 * Still not exactly like clicking the “Publish” button, but I think it will have
   the desired effect. One odd thing about scheduled events is that some sort of
   WP request needs to happen in order for the event to execute. The event may not
   execute exactly on schedule on low traffic sites. On local test installations
   the event will not execute at all unless you load a page to trigger the script.
   On live sites there should be enough search bot traffic such that live visitors
   are not needed.
 *  Thread Starter [D C](https://wordpress.org/support/users/derekryanclarke/)
 * (@derekryanclarke)
 * [10 years, 4 months ago](https://wordpress.org/support/topic/sql-script-for-publishing/#post-6940658)
 * Am I way off or something like this?
 *     ```
       $my_post = array(
         'post_type'       = 'post',
         'post_status'      = 'publish'
         );
         wp_update_post( $my_post );
       ```
   
 * I feel like I am missing something… Like stating where post_status = draft
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [10 years, 4 months ago](https://wordpress.org/support/topic/sql-script-for-publishing/#post-6940663)
 * At the very least you need a post ID in order to use `wp_update_post()`. Use `
   get_posts()` or create a [new WP_Query](http://codex.wordpress.org/Class_Reference/WP_Query)
   object to get all draft posts. Then loop through the results to update the status.
 * You may want to qualify draft posts that you do not want published with something
   like a special category or tag. When you periodically get all draft posts, exclude
   such posts from the query.
 *  Thread Starter [D C](https://wordpress.org/support/users/derekryanclarke/)
 * (@derekryanclarke)
 * [10 years, 4 months ago](https://wordpress.org/support/topic/sql-script-for-publishing/#post-6940664)
 * Thanks BCW. I will circle back around after I spend some time on this today. 
   I appreciate your help.
 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [10 years, 4 months ago](https://wordpress.org/support/topic/sql-script-for-publishing/#post-6940666)
 * I saw in your other topic on this issue that the plugin failed to trigger when
   updating the status this way. Very disappointing! There are a few other actions
   that fire when actually publishing a post via the edit screen, I imagine the 
   plugin is using one of these.
 * I suppose you could trace through all the code that executes from where ever 
   the edit form submits to, and test fire every hook encountered until you find
   which one works. Then fire that hook with your scheduled task along with changing
   the status.
 *  Thread Starter [D C](https://wordpress.org/support/users/derekryanclarke/)
 * (@derekryanclarke)
 * [10 years, 4 months ago](https://wordpress.org/support/topic/sql-script-for-publishing/#post-6940677)
 * Yeah that may be the last resort. Trying to dig deeper into the plugin code. 
   There may be a way to put it on a schedule then all I can skip the draft to publish.

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘SQL script for publishing’ is closed to new replies.

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 10 replies
 * 2 participants
 * Last reply from: [D C](https://wordpress.org/support/users/derekryanclarke/)
 * Last activity: [10 years, 4 months ago](https://wordpress.org/support/topic/sql-script-for-publishing/#post-6940677)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
