Skip to content

Instantly share code, notes, and snippets.

@derweili
Last active January 28, 2022 09:51
Show Gist options
  • Save derweili/22687cbeffb5f0a50bf7c9c5df9a9633 to your computer and use it in GitHub Desktop.
Save derweili/22687cbeffb5f0a50bf7c9c5df9a9633 to your computer and use it in GitHub Desktop.
WordPress Share a Draft Custom Post Type Support
<?php
/**
* Plugin Name: Share A Draft Helper
* Plugin URI: derweili.de
* Description: Add Custom Post Type Support to the Share A Draft Plugin
* Author: derweili
* Author URI: derweili.de
* Text Domain: share-a-draft-helper
* Domain Path: /languages
* Version: 0.1.0
*/
/**
* Add Custom Post Type to Draft Select Dropdown
*
* @see https://wordpress.org/support/topic/custom-post-type-support-solution/
*/
add_filter('pre_get_posts',"sad_helper_add_cpt_to_dropdown");
function sad_helper_add_cpt_to_dropdown($query) {
if(!is_admin()) return;
$screen = get_current_screen();
if($screen->base != "posts_page_shareadraft/shareadraft") return;
$query->set('post_type',array('post','my-custom-post-type')); // Add your post types to this array
}
/**
* Manipulate the sql query where WordPress looks for posts.
*
* The Share a Draft plugin hooks in very early using the
* `posts_results` where WordPress only searches for the
* default `post` post-type.
* This can only be changed by directly manipulating the SQL WHERE clause
*/
add_filter('posts_clauses_request', function( $clauses ) {
// only manipulate the sql query when a share-a-draft link is requested
if( ! isset( $_GET['shareadraft'] ) ) return $clauses;
// Change the SQL where clause
$clauses['where'] = str_replace(
"post_type = 'post'",
"post_type IN ('post', 'my-custom-post-type')", // Add your post types to this where clause
$clauses['where']
);
return $clauses;
}, 10, 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment