Skip to content

Instantly share code, notes, and snippets.

@fabienlege
Last active February 5, 2020 17:45
Show Gist options
  • Save fabienlege/d39d12685ac6685da7d1ce5807418d29 to your computer and use it in GitHub Desktop.
Save fabienlege/d39d12685ac6685da7d1ce5807418d29 to your computer and use it in GitHub Desktop.
Completely disable built-in posts in wordpress
<?php
/***
* _ _____
* | | | ___|
* __ _ __ _ ___ _ __ ___ ___ | |__ ___ |___ \
* / _` |/ _` |/ _ \ '_ \ / __/ _ \ | '_ \ / _ \ \ \
* | (_| | (_| | __/ | | | (_| __/ | | | | (_) /\__/ /
* \__,_|\__, |\___|_| |_|\___\___| |_| |_|\___/\____/
* __/ |
* |___/
*
* >> https://agenceho5.com
*/
/**
* /====================================\
* | |
* | /!\ IMPORTANT WARNING /!\ |
* | |
* \====================================/
*
* WARNING IF YOU USE THIS THIS GIST !!
* this gist modify the wordpress default behavior of the get_posts function !
*
* - by default, get_posts define `suppress_filters` parameter to true. In this code, I turn it to false.
* - `suppress_filters` parameter only affect `posts_*` filters
* - So, if you use `posts_*` filters anywhere on you website, please check if that gist not impact query results of your get_posts calls
*/
/**
* Basic instruction for disabling wordpress built-in post type but not hide all posts on front
*/
add_action( 'init', function () {
global $wp_post_types;
$wp_post_types['post']->public = false;
$wp_post_types['post']->exclude_from_search = true;
$wp_post_types['post']->publicly_queryable = false;
$wp_post_types['post']->show_ui = false;
$wp_post_types['post']->show_in_menu = false;
$wp_post_types['post']->show_in_nav_menus = false;
$wp_post_types['post']->show_in_admin_bar = false;
$wp_post_types['post']->show_in_rest = false;
}, 0 );
/**
* Hide all existings posts on front
*/
add_filter( 'posts_where', function ( $where ) {
return $where . " AND wp_posts.post_type != 'post'";
}, 0 );
/**
* If `get_posts` function is used somewhere, `post_where` filter isn't executed by default.
*/
add_action( 'pre_get_posts', function ( $query ) {
$query->set( 'suppress_filters', false );
}, 0 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment