0

I am creating a plugin which registers a custom post type called 'news articles' see the class for this code below:

<?php
/**
 * This plugin class is used to register the necessary custom post types
*/
class Dw_Mf_Register_CPT {

/**
 * Register Custom Post Types
 *
 * @since 1.0.0
 * @access public
 * @return void
 */
public function register_cpt(){
    add_action( 'init', array( $this, 'create_news_article_cpt') );
}

/**
 * Add News Article Custom Post Type
 *
 * @since 1.0.0
 * @access public
 * @return void
 */
public function create_news_article_cpt(){

    $supports = array(
        'title', // post title
        'editor', // post content
        'author', // post author
        'thumbnail', // featured images
        'excerpt', // post excerpt
        'custom-fields', // custom fields
        'comments', // post comments
        'revisions', // post revisions
        'post-formats', // post formats
        );
        $labels = array(
        'name' => _x('news articles', 'plural'),
        'singular_name' => _x('news article', 'singular'),
        );
        $args = array(
        'supports' => $supports,
        'labels' => $labels,
        'public' => true,
        'query_var' => true,
        'rewrite' => array('slug' => 'news-articles'),
        'has_archive' => true,
        'hierarchical' => false,
        );
        register_post_type('news articles', $args);
}
}

It works on the back end. I am able to see the custom post type in the menu and I am able to create new news articles.

However, when I try to view any of these news articles I am 301 redirected to the homepage.

When I take the same code and remove it from a PHP class context and put it directly into functions.php as a standard php function and register the custom post type it works as expected and I am able to view the news articles I create on the front of the website without them redirecting to the homepage.

//functions.php code
function create_post_type_news() {
$supports = array(
    'title', // post title
    'editor', // post content
    'author', // post author
    'thumbnail', // featured images
    'excerpt', // post excerpt
    'custom-fields', // custom fields
    'comments', // post comments
    'revisions', // post revisions
    'post-formats', // post formats
    );
    $labels = array(
    'name' => _x('news articles', 'plural'),
    'singular_name' => _x('news article', 'singular'),
    );
    $args = array(
    'supports' => $supports,
    'labels' => $labels,
    'public' => true,
    'query_var' => true,
    'rewrite' => array('slug' => 'news-articles'),
    'has_archive' => true,
    'hierarchical' => false,
    );
    register_post_type('news articles', $args);
}
add_action('init', 'create_post_type_news');

Ideally, this would work in a plugin and I wouldn't need to register the CPT in the functions.php file. Any insight on what the problem might be?

  • You have space in custom post type slug: register_post_type('news articles', $args);. – nmr Aug 7 at 11:47
0
  1. Your class has no __construct() method.
  2. Even if it did, you're not calling the class to set it up

So...

1) at the top of your class, add:

function __construct() {
    register_cpt();
}

2) after your class, add:

$myCPT = new Dw_Mf_Register_CPT();

Or, you could just get rid of the class construction altogether. You don't actually need it if all your plugin does is add the CPT.

  • Thanks I will try this. The plugin does more this is just one class from it. – dan webb Aug 7 at 11:47

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.