Custom Post Type causes Page Not Found

I found a basic tutorial online which gave the rundown on how to create a plugin from scratch and sets up a new custom post type. I could not find any basic coupon plugins that accomplished what I wanted, why I decided to look into creating one myself.

Unfortunately after completing the tutorial, I came to the part where I actually view the page, only to be greeted with a “Page Not Found” despite publishing my post. Can anyone check this out and let me know exactly what I am doing wrong?

<?php

/**
* Plugin Name: Coupons
* Plugin URI: http://coupon.net
* Description: A coupon system plugin
* Version: 1.0.0
* Author: Brett Powell
* Author URI: http://coupon.net
**/

// Register the Custom Post Type "Coupon"
function register_cpt_coupon()
{
    $labels = array(
        'name' => _x( 'Coupons', 'coupon' ),
        'singular_name' => _x( 'Coupon', 'coupon' ),
        'add_new' => _x( 'Add New', 'coupon' ),
        'add_new_item' => _x( 'Add New Coupon', 'coupon' ),
        'edit_item' => _x( 'Edit Coupon', 'coupon' ),
        'new_item' => _x( 'New Coupon', 'coupon' ),
        'view_item' => _x( 'View Coupon', 'coupon' ),
        'search_items' => _x( 'Search Coupons', 'coupon' ),
        'not_found' => _x( 'No coupons found', 'coupon' ),
        'not_found_in_trash' => _x( 'No coupons found in Trash', 'coupon' ),
        'parent_item_colon' => _x( 'Parent Coupon:', 'coupon' ),
        'menu_name' => _x( 'Coupons', 'coupon' ),
    );

    $args = array(
        'labels' => $labels,
        'hierarchical' => true,
        'description' => 'Coupons filterable by company',
        'supports' => array( 'title', 'editor', 'thumbnail', 'revisions', 'page-attributes' ),
        'taxonomies' => array( 'genres' ),
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 5,
        'menu_icon' => 'dashicons-format-audio',
        'show_in_nav_menus' => true,
        'publicly_queryable' => true,
        'exclude_from_search' => false,
        'has_archive' => true,
        'query_var' => true,
        'can_export' => true,
        'rewrite' => true,
        'capability_type' => 'post'
    );

    register_post_type( 'coupon', $args );
}

add_action( 'init', 'register_cpt_coupon' );

// Create Genre Taxonomy type
function genres_taxonomy()
{
    register_taxonomy(
        'genres',
        'coupon',
        array(
            'hierarchical' => true,
            'label' => 'Genres',
            'query_var' => true,
            'rewrite' => array(
                'slug' => 'genre',
                'with_front' => false
            )
        )
    );
}
add_action( 'init', 'genres_taxonomy');

// Function used to automatically create Coupon page.
function create_coupon_pages()
{
   //post status and options
    $post = array(
          'comment_status' => 'open',
          'ping_status' =>  'closed' ,
          'post_date' => date('Y-m-d H:i:s'),
          'post_name' => 'coupon',
          'post_status' => 'publish' ,
          'post_title' => 'Coupons',
          'post_type' => 'page',
    );
    //insert page and save the id
    $newvalue = wp_insert_post( $post, false );

    //save the id in the database
    update_option( 'couponpage', $newvalue );
}

// // Activates function if plugin is activated
register_activation_hook( __FILE__, 'create_coupon_pages');

?>

1 Answer
1

You quite likely need to simply visit your permalink settings page to refresh. Goto Admin > Settings > Permalinks. Then try your URL on the front-end again.

Leave a Comment