0

I have a little helper class for registering new custom post types. However, it seems it is registering the custom post type on each page load. I put in a small check for this:

if( ! post_type_exists( $this->post_type_name ) )
{
  add_action( 'init', array( &$this, 'register_post_type' ) );
}

However the problem persists. I added

var_dump(post_type_exists( $this->post_type_name ));

to the line just before, but this always seems to return false, even though my custom post types are working. Can anyone explain this. Below is my full class, and the initial plugin file.

index.php file

require_once plugin_dir_path( __FILE__ ) . 'admin/class-custom-post-type.php';

$location = new LegendPlugin\WP\Classes\CustomPostType('Location');
$project  = new LegendPlugin\WP\Classes\CustomPostType('Project');
$project->add_taxomony('Type');
$project->add_taxomony('Sector');

And the CustomPostType class:

namespace LegendPlugin\WP\Classes;

class CustomPostType {

  private $post_type_name;
  private $args;
  private $labels;
  private $textDomain;

  public function __construct( $name, $args = array(), $labels = array() )
  {
    $this->post_type_name = strtolower( str_replace( ' ', '_', $name ) );
    $this->args = $args;
    $this->labels = $labels;
    // Add action to register the post type, if the post type does not already exist

    var_dump(post_type_exists( $this->post_type_name ));
    if( ! post_type_exists( $this->post_type_name ) )
    {
      add_action( 'init', array( &$this, 'register_post_type' ) );
    }
  }

  public function register_post_type()
  {
    $post_type_name = ucwords($this->post_type_name);
    $post_type_name_plural = $post_type_name . 's';

    $labels = array_merge(

      array(
        'name'                  => _x( $post_type_name_plural, 'post type general name' ),
        'singular_name'         => _x( $post_type_name, 'post type singular name' ),
        'add_new'               => _x( 'Add New', strtolower( $post_type_name ) ),
        'add_new_item'          => __( 'Add New ' . $post_type_name ),
        'edit_item'             => __( 'Edit ' . $post_type_name ),
        'new_item'              => __( 'New ' . $post_type_name ),
        'all_items'             => __( 'All ' . $post_type_name_plural ),
        'view_item'             => __( 'View ' . $post_type_name ),
        'search_items'          => __( 'Search ' . $post_type_name_plural ),
        'not_found'             => __( 'No ' . strtolower( $post_type_name_plural ) . ' found'),
        'not_found_in_trash'    => __( 'No ' . strtolower( $post_type_name_plural ) . ' found in Trash'),
        'parent_item_colon'     => '',
        'menu_name'             => $post_type_name_plural
      ),

      $this->labels
    );

    $args = array_merge(
      array(
        'label'                 => $post_type_name,
        'labels'                => $labels,
        'public'                => true,
        'show_ui'               => true,
        'supports'              => array( 'title', 'editor' ),
        'show_in_nav_menus'     => true,
        '_builtin'              => false,
      ),

      $this->args
    );

    // Register the post type
    register_post_type( $this->post_type_name, $args );

    // Flus the rewrite rules to prevent problems
    flush_rewrite_rules(false);
  }

  public function add_taxomony( $name, $args = array(), $labels = array() )
  {
    if( ! empty( $name ) )
    {
      $post_type_name = $this->post_type_name;

      // Taxonomy properties
      $taxonomy_name      = strtolower( str_replace( ' ', '_', $name ) );
      $taxonomy_labels    = $labels;
      $taxonomy_args      = $args;
    }

    if(  ! taxonomy_exists( $taxonomy_name ) )
    {
      $name       = ucwords( str_replace( '_', ' ', $name ) );
      $plural     = $name . 's';

      $labels = array_merge(
        array(
          'name'                  => _x( $plural, 'taxonomy general name' ),
          'singular_name'         => _x( $name, 'taxonomy singular name' ),
          'search_items'          => __( 'Search ' . $plural ),
          'all_items'             => __( 'All ' . $plural ),
          'parent_item'           => __( 'Parent ' . $name ),
          'parent_item_colon'     => __( 'Parent ' . $name . ':' ),
          'edit_item'             => __( 'Edit ' . $name ),
          'update_item'           => __( 'Update ' . $name ),
          'add_new_item'          => __( 'Add New ' . $name ),
          'new_item_name'         => __( 'New ' . $name . ' Name' ),
          'menu_name'             => __( $plural ),
        ),

        $taxonomy_labels
      );

      $args = array_merge(

        array(
          'label'                 => $plural,
          'labels'                => $labels,
          'public'                => true,
          'show_ui'               => true,
          'show_in_nav_menus'     => true,
          '_builtin'              => false,
        ),

        $taxonomy_args
      );

      // Add the taxonomy to the post type
      add_action( 'init', function() use( $taxonomy_name, $post_type_name, $args ) {
          register_taxonomy( $taxonomy_name, $post_type_name, $args );
          // Flus the rewrite rules to prevent problems
          flush_rewrite_rules(false);
        }
      );

    }
    else
    {
      // Taxonomy is already registered
      // just attach it to the post type
      add_action( 'init', function() use( $taxonomy_name, $post_type_name ) {
          register_taxonomy_for_object_type( $taxonomy_name, $post_type_name );
          // Flus the rewrite rules to prevent problems
          flush_rewrite_rules(false);
        }
      );
    }

  }

}
0

You must call register_post_type on each page load, when you register a post type it doesn't modify the database it updates variables which only exist for that page load. If you don't do this WordPress is unaware of that post type. Much the same way that you register settings pages and styles on every page load, WordPress doesn't remember that you enqueued a stylesheet 5 pages ago, it needs to be told to load it.

It's the same with post types and custom taxonomies. Even the default types of pages posts categories and tags are registered on each load.

As a side note, you should not flush rewrite rules on every page load, it's an expensive operation and only needs to be done when things change, as rewrite rules are stored in the database.

  • So how would I go about flushing the rules after a custom post type is registered? The first time, the rules need to be flushed or the user could get 404 error problems. – WebDevDude Nov 23 '14 at 17:36
  • Plugin activation is a popular one, usually the user needs to visit the permalink page and the rules are flushed automatically – Tom J Nowell Nov 23 '14 at 17:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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