Git Product home page Git Product logo

Comments (9)

jjgrainger avatar jjgrainger commented on September 23, 2024

At a quick glance, everything looks perfect except for the CPT name.

The CPT name needs to be all lowercase and spaces replaced with underscores as its not a label but a name used by Wordpress to identify the post type. This might be causing the problems you're having when registering taxonomies.

Could you try this for me and let me know how you get on,

$staff = new CPT(array(
    'post_type_name' => 'staff_member',
    'singular' => 'Staff Member',
    'plural' => 'Staff Members',
    'slug' => 'staff-members'
), $options);

$staff->register_taxonomy('departments');

Thanks :)

from wp-custom-post-type-class.

joelataylor avatar joelataylor commented on September 23, 2024

I was having the same problem. There's a difference between these two initiation calls:

$staff = new CPT(array(
    'post_type_name' => 'staff_member',
    'singular' => 'Staff Member',
    'plural' => 'Staff Members',
    'slug' => 'staff-members'
), $options);

vs

$staff = new CPT('Staff Member', array(
    'post_type_name' => 'staff_member',
    'singular' => 'Staff Member',
    'plural' => 'Staff Members',
    'slug' => 'staff-members'
), $options);

from wp-custom-post-type-class.

jjgrainger avatar jjgrainger commented on September 23, 2024

Hi guys,

I've just tested the solution I supplied previously and it all seems to work fine.

The problem identified in @loremipson example is using spaces and capital letters in the post type name.

The post type name is used to register the post type with Wordpress and not used for labels. On the Wordpress Codex for the register_post_type function, which the class uses, it states that the $post_type parameter cannot contain spaces or capital letters (see here).

If you do supply an incorrect name for the post type, Wordpress removes the spaces and turns the string to all lowercase, so in @loremipson example 'Staff Members' was converted to 'staffmembers'.

The class doesn't recognise this and trys to register the taxonomy to a post type called 'Staff Members', which doesn't exisit, and therfore doesn't register the taxonomy.

@joelataylor, @loremipson If you could both try the solution I posted previously and let me know how you get on that would be great.

@joelataylor If you're are still having trouble it would be good to see some code examples to get a better idea of what you're trying to do.

Hope that helps,

Thanks guys :)

from wp-custom-post-type-class.

joelataylor avatar joelataylor commented on September 23, 2024

@jjgrainger - dah! You're totally right. I didn't realize the first param is the WP $post_type and therefore constained to the specifications you pointed out. If I change back to:

$menus = new CPT('menu', array(
            'supports'  => array( 'title' ),
            'public' => true,
            'has_archive' => true,
            'rewrite' => array('slug' => 'menus', 'with_front' => false),
            'menu_icon' => 'dashicons-media-spreadsheet'
        ) );

        $menus->register_taxonomy(array(
            'taxonomy_name' => 'menu-type',
            'singular' => 'Menu Type',
            'plural' => 'Menu Types',
            'slug' => 'menu-type'
        ), array(
            'public' => false,
            'show_ui' => true,
            'hierarchical' => false
        ));

Everything works again. :) Thanks for the support!

from wp-custom-post-type-class.

jjgrainger avatar jjgrainger commented on September 23, 2024

@joelataylor no worries, glad you got it working :)

from wp-custom-post-type-class.

joelataylor avatar joelataylor commented on September 23, 2024

I think because the former (incorrect) call to the initiation call still worked, the issue only really presented itself when trying to add a taxonomy.

Maybe in the next revision, some error checking of that field would be appropriate. But still, lesson learnt: Read the docs!!!

from wp-custom-post-type-class.

QWp6t avatar QWp6t commented on September 23, 2024

I have my own CPT class similar to this, and I actually make the first field able to accept a name with spaces and whatnot, and then I use WordPress functions to turn it into a slug to make it compatible as an identifier.

public function __construct($name, $options = [], $id = '')
{
    empty($id) && $id = sanitize_title($name);
    // ...
}

from wp-custom-post-type-class.

QWp6t avatar QWp6t commented on September 23, 2024

If you want to roll your own, it would look something like this (untested)...

    /**
     * @param $str
     * @return string
     */
    public static function generateId($str)
    {
        $str = self::removeAccentsFromString($str); // you'd have to write something like this or find it
        $str = strtolower($str);
        $str = preg_replace('/[^a-z0-9 \-_]/','',$str);
        return str_replace([' ','_'], '-', $str); // underscores are technically fine, but I just dislike them. lol
    }

from wp-custom-post-type-class.

jjgrainger avatar jjgrainger commented on September 23, 2024

Thanks for the suggestions but I would argue against automatically correcting the post type name as I can only see it creating confusion.

The post type name is used not only for registering the post type, but also creating specific templates (single-posttype.php) and referencing the post type in Wordpress API's such as WP_Query.

For example if someone tries to create a post type as 'Staff Members' and the class automatically registers the post type as 'staff_members', how is the developer supposed to know?

People may end up trying to reference the post type like so:

$loop = new WP_Query(array(
    'post_type' => 'Staff Members'
));

And find it not to work as they should be referencing staff_members.

Reading through the documentation it doesn't come across and clearly as it should, so I think the best course of action is to work on the documentation to make it clearer at a glance.

Thanks for your input everyone :)

from wp-custom-post-type-class.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.