How do I add a custom post type to what I'm assuming is a table in WP's underlying database.

I.e. I don't want to have this loading on every page and I want to be able to use slug and post-type queries on it.

function cptui_register_my_cpts_app() {

    $labels = array();
    $args = array();
    register_post_type( "app", $args );

add_action( 'init', 'cptui_register_my_cpts_app' );

More specifically, I want to be able to create a redirect from a custom post type (in this case 'app') to the login page if members are not already logged in.

function my_redirect() {
    if( !is_user_logged_in() && is_singular('app') ) {
        wp_redirect( 'login page' );
add_action('init', 'my_redirect');

3 Answers 3


The built in post type definitions aren't stored in the database, they're registered on every request, just like custom types. There's no way around this.

If you just want to check the request and your logged in state as early as possible, you can use the parse_request action:

function wpd_parse_request( $request ) {
    if( !is_user_logged_in() && isset( $request->query_vars['app'] ) ){
        wp_redirect( wp_login_url() );
add_action( 'parse_request', 'wpd_parse_request' );

Custom Post types are automatically stored in the wp_posts table along with all other posts. However you cannot use all the normal queries to access them. I believe this is what you are looking for pre_get_posts hook. According to the codex

This hook is called after the query variable object is created, but before the actual query is run. The pre_get_posts action gives developers access to the $query object by reference

Below is a very nice snippet that will allow you to use normal post queries for your custom post types. Found from here. (untested personally, I had come across it a few days ago. Haven't got the chance to try it yet.)

 * Extend wp search to include custom post meta 

function custom_search_query( $query ) {
    if ( !is_admin() && $query->is_search ) {
        $query->set('meta_query', array(
                'key' => '__meta_key__',
                'value' => $query->query_vars['s'],
                'compare' => 'LIKE'
         $query->set('post_type', '__your_post_type__'); // optional
add_filter( 'pre_get_posts', 'dc_custom_search_query');

You can do this using the WP_Query class and the post_type argument. So in your case with the machine readable name of app given to your custom post type:

$args = array(
    'post_type' => 'app',

$my_query = new WP_Query( $args );

Then you just feed it into the Loop:


    if ( $my_query->have_posts() ) {
        while ( $my_query->have_posts() ) : $my_query->the_post(); ?>

        // Your Loop Code

        <?php endwhile; ?>

        <?php wp_reset_postdata(); ?>

    <?php else : ?>
        // No Posts Found Code
    <?php endif; ?>


You can use other arguments to further refine your results. Just make sure your custom post type declaration has public => true, in its arguments.

That being said, your definition of custom post types, any associated custom taxonomies, etc. should actually be in plugins. That way you can swap themes in and out at will without orphaning your custom posts and taxonomies or have to port the code into a new theme each time.

Your Answer

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

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