• Hi there. Could someone check to see if my custom post type has been created properly. This is my first attempt using a tutorial from wp.tutes

    This is my custom post type code;

    <?php
    /*
    Plugin Name: Jobs
    Description: Plugin for Inside Biz Ed
    Version: 1.0
    Author: Alex Johnson
    Author URI: https://webfeetdesign.com/
    */
    
    add_action( 'init', 'create_job' );
    
    function create_job() {
        register_post_type( 'job_post',
            array(
                'labels' => array(
                    'name' => 'Job Posts',
                    'singular_name' => 'Job Post',
                    'add_new' => 'Add New',
                    'add_new_item' => 'Add New Job Post',
                    'edit' => 'Edit',
                    'edit_item' => 'Edit Job Post',
                    'new_item' => 'New Job Post',
                    'view' => 'View',
                    'view_item' => 'View Job Post',
                    'search_items' => 'Search Job Posts',
                    'not_found' => 'No Job Posts found',
                    'not_found_in_trash' => 'No Job Posts found in Trash',
                    'parent' => 'Parent Job Post'
                ),
    
                'public' => true,
                'menu_position' => 15,
                'supports' => array( 'title', 'editor', 'thumbnail' ),
                'taxonomies' => array( '' ),
                'menu_icon' => plugins_url( 'images/jobs-icon.png', __FILE__ ),
                'has_archive' => true
            )
        );
    }
    
    add_action( 'admin_init', 'my_admin' );
    
    function my_admin() {
        add_meta_box( 'job_post_meta_box',
            'Job Post Details',
            'display_job_post_meta_box',
            'job_post', 'normal', 'high'
        );
    }
    
    function display_job_post_meta_box( $job ) {
        // Retrieve current name of the employer based on job ID
        $job_employer = esc_html( get_post_meta( $job->ID, 'job_employer', true ) );
    	// Retrieve current name of the location based on job ID
        $job_location = esc_html( get_post_meta( $job->ID, 'job_location', true ) );
    	// Retrieve current name of the position based on job ID
        $job_position = esc_html( get_post_meta( $job->ID, 'job_position', true ) );
    	// Retrieve current name of the salary based on job ID
        $job_salary = esc_html( get_post_meta( $job->ID, 'job_salary', true ) );
    	// Retrieve current name of the end date based on job ID
        $job_end = esc_html( get_post_meta( $job->ID, 'job_end', true ) );
        ?>
        <table>
            <tr>
                <td style="width: 100%">Job Employer</td>
                <td><input type="text" size="80" name="job_post_employer" value="<?php echo $job_employer; ?>" /></td>
            </tr>
            <tr>
                <td style="width: 100%">Job Location</td>
                <td><input type="text" size="80" name="job_post_location" value="<?php echo $job_location; ?>" /></td>
            </tr>
            <tr>
                <td style="width: 100%">Contract</td>
                <td><input type="text" size="80" name="job_post_position" value="<?php echo $job_position; ?>" /></td>
            </tr>
            <tr>
                <td style="width: 100%">Job Salary</td>
                <td><input type="text" size="80" name="job_post_salary" value="<?php echo $job_salary; ?>" /></td>
            </tr>
            <tr>
                <td style="width: 100%">Expiry Date</td>
                <td><input type="text" size="80" name="job_post_end" value="<?php echo $job_end; ?>" /></td>
            </tr>
        </table>
        <?php
    }
    
    add_action( 'save_post', 'add_job_post_fields', 10, 2 );
    
    function add_job_post_fields( $job_id, $job ) {
        // Check post type for job posts
        if ( $job->post_type == 'job_post' ) {
            // Store data in post meta table if present in post data
            if ( isset( $_POST['job_post_employer'] ) && $_POST['job_post_employer'] != '' ) {
                update_post_meta( $job_id, 'job_employer', $_POST['job_post_employer'] );
            }
            if ( isset( $_POST['job_post_location'] ) && $_POST['job_post_location'] != '' ) {
                update_post_meta( $job_id, 'job_location', $_POST['job_post_location'] );
            }
            if ( isset( $_POST['job_post_position'] ) && $_POST['job_post_position'] != '' ) {
                update_post_meta( $job_id, 'job_position', $_POST['job_post_position'] );
            }
            if ( isset( $_POST['job_post_salary'] ) && $_POST['job_post_salary'] != '' ) {
                update_post_meta( $job_id, 'job_salary', $_POST['job_post_salary'] );
            }
            if ( isset( $_POST['job_post_end'] ) && $_POST['job_post_end'] != '' ) {
                update_post_meta( $job_id, 'job_end', $_POST['job_post_end'] );
            }
        }
    }
    
    add_filter( 'template_include', 'include_template_function', 1 );
    
    function include_template_function( $template_path ) {
        if ( get_post_type() == 'job_post' ) {
            if ( is_single() ) {
                // checks if the file exists in the theme first,
                // otherwise serve the file from the plugin
                if ( $theme_file = locate_template( array ( 'single-job_post.php' ) ) ) {
                    $template_path = $theme_file;
                } else {
                    $template_path = plugin_dir_path( __FILE__ ) . '/single-job_post.php';
                }
            }
        }
        return $template_path;
    }
    
    ?>

    It seems to work ok but I can’t get it to display correctly. I have a page template in wordpress call Jobs. This displays the excerpt fine but then when I click on it to see the full job post I get a “Not Found” page. See here – https://insidebized.com/jobs/

    I really hope someone can help me here as its dragged on for weeks now.

    Many thanks

  • The topic ‘Can't display my custom post type’ is closed to new replies.