My personal site at jjgrainger.co.uk. Built with Jekyll, hosted on Netlify.
jjgrainger / wp-custom-post-type-class Goto Github PK
View Code? Open in Web Editor NEWA PHP Class for creating Wordpress Custom Post Types easily
License: MIT License
A PHP Class for creating Wordpress Custom Post Types easily
License: MIT License
My personal site at jjgrainger.co.uk. Built with Jekyll, hosted on Netlify.
I don't believe this is currently possible. It'd be useful to be able to override the default post type labels. I often like to set a custom menu_name for example.
I think the 'supports' default array needs to be empty. I want a CPT with only a title, but because the default array contains 'title', 'editor' and 'thumbnail', these still get included with the merge. I may be doing something wrong, but was curious if anyone else has seen this.
I think line 508 should be:
'supports' => array(),
Am I missing something?
I've just upgraded to v1.3.2 and am seeing the following error.
Warning: Invalid argument supplied for foreach() in /var/www/project/vendor/jjgrainger/wp-custom-post-type-class/src/CPT.php on line 568
When saving/updating a post the default post update message appears
see customising messages example: http://codex.wordpress.org/Function_Reference/register_post_type#Example
To fix this I just added this to the default options for now, but I think the option merge needs looking at
I frequently disable the CPT archive and use a custom page template instead - basically any time the classic blog list format doesn't apply.
Will try to hack it in real quick but if I submit a pull request, it may not be how you'd do it within your structure.
When I try to use this class in a custom plugin, it won't register on add_action('init'). I need to use add_action('plugins_loaded') to get it to register, but that doesn't feel ok.
If I replace $book->CPT(...) with wordpress own register_post_type, it works on add_action('init').
Any ideas?
The query created when attempting sortable admin columns using automatic taxonomies doesn't work:
array (
'order' => 'asc',
'orderby' => 'meta_value',
'posts_per_page' => 200,
'post_type' => 'sm-item',
'meta_key' => 'sm-item-section',
)
Wasn't clear from the docs that only meta values were supported.
Might be a workable solution here:
http://wordpress.stackexchange.com/questions/14306/using-wp-query-is-it-possible-to-orderby-taxonomy
Can I use this class with already registered post types, such as 'Posts'. I would like to use the class to register taxonomies and use the other features.
We're getting a Invalid argument supplied for foreach()
on line 568. We've just updated to the latest version released yesterday 1.3.2
.
Hello,
You class is not supported with 5.2.17 version but WordPress support it. So please help out to fix this for 5.2.17 php version.
I would say your class is fantastic. I am very satisfied using CPT Class. I would like to know if it might be possible to implement some methods to change the navigation of bulk-action-selector-top, or the actions of the date filter selector. Example: Show ACF datepicker in the dropdown date filter.
Hello:
Thank you for the great work on this class. Makes setup and config so much easier for us. I was wondering if there's a way to add post_row_actions
to a custom column? I have a CPT for 'people' that is not using the default post title, and instead I've created a column that displays last_name, first_name
and I'd like to have row actions (pictured) available on hover if possible.
Thanks for any help you can offer.
This showed up in a deploy to a wordpress site, but I have deployed same to other servers without this error...
Fatal error: Call to undefined function array_replace_recursive() in /home/content/j/o/s/sitenamehere/html/wp-content/themes/themename/inc/post-types/CPT.php on line 512
I just read that wp-api 2 does not supports from default custom type. This one needs to be enabled by passed $args like 'show_in_rest' => true,
. How would I do that?
Hello
I was wondering whether it is possible to set the menu_position of the cpt 'post'?
It would be nice to add extra fields to a taxonomy.
I may be missing something here.. Wordpress is something I only touch a couple times a year, so I'm very rusty.
I have an $options
array defined, then:
$staff = new CPT('Staff Member', $options);
$staff->register_taxonomy('departments');
That should work, correct? Or is it more involved than this? It's not adding the "Staff Members -> Departments" menu option in the Wordpress ui at all. I can add the existing "Categories" taxonomy to post types, but registering new ones doesn't seem to do anything.
No error messages or anything, so I get the vibe that I'm overlooking a step. I've also tried calling it via the $options
taxonomies array too, but no success there either..
Thanks ahead of time
Add contributing guide to repo
examples
more links
Reference https://github.com/composer/installers
somehow even when add_theme_support('post-thumbnails');
is activated, Featured Image isn't showing up.
Using code similar to below:
$enquiries->columns(array(
'cb' => '<input type="checkbox" />',
'name' => __('Name'),
'email' => __('Email'),
'telephone' => __('Telephone')
));
$enquiries->populate_column('name', function($column, $post) {
echo get_field('name');
});
$enquiries->populate_column('email', function($column, $post) {
echo get_field('email');
});
$enquiries->populate_column('telephone', function($column, $post) {
echo get_field('telephone');
});
From version 1.3
to 1.3.1
the added default columns seems to break all columns registered by other plugins and if no custom columns are registered as well.
In order to get around this I currently set the first defaults columns array to $default and then did a $columns = array_replace_recursive( $defaults, $columns );
Not sure that's to final and best way, but it currently takes care of the issue.
Thank you your Class. It's very well done
I want to add some fields, example price
How do I add field price ?
Thanks & Regards
Hi mate
Very cool class, but the menu icons aren't working for me. My IDE isn't actually recognising $this->menu_icon['hover']
as available from within the set_post_icon()
method.
I'm setting up the CPT like so;
$teams = new CPT(array(
'post_type_name' => 'grdl_team',
'singular' => 'Team',
'plural' => 'Teams',
'slug' => 'teams'
),
array(
'supports' => array('title', 'editor'),
'hierarchical' => false,
'has_archive' => true
));
$teams->menu_icon('dashicons-book-alt');
The logo doesn't change in the admin and I get the following errors;
[06-Jan-2014 23:57:31 UTC] PHP Notice: Undefined property: CPT::$menu_icon in /xxx/wp-content/themes/pds-master/library/vendor/CPT.php on line 1148
[06-Jan-2014 23:57:31 UTC] PHP Notice: Undefined property: CPT::$menu_icon in /xxx/wp-content/themes/pds-master/library/vendor/CPT.php on line 1153
[06-Jan-2014 23:57:31 UTC] PHP Notice: Undefined property: CPT::$menu_icon in /xxx/wp-content/themes/pds-master/library/vendor/CPT.php on line 1157
I cant seem to find a way to register these with Archives. Im not seeing it anywhere in the source code either.
This can only register post types as "pages" without archives it seems.
I can't seem to be able to register a custom tax to my CPT. Here my code in the simplest form possible, but yet it won't show up in the admin screen.
There was a similar issue about a year ago, but I tripled checked my code and all seems to be in order.
https://github.com/jjgrainger/wp-custom-post-type-class/issues/25
What am I not seeing?
$movies = new CPT( array(
'post_type_name' => 'movie',
'singular' => 'Movie',
'plural' => 'Movies',
'slug' => 'movies',
));
$movies->register_taxonomy('genre');
$movies->register_post_type();
$movies->flush();
It seams that this repository is not automatically synced into GitHub. Maybe you could add the GitHub Service Hook in the repository settings? Then you wont need to visit packagist for each new release.
GitHub Service Hook
Enabling the Packagist service hook ensures that your package will always be updated instantly when you push to GitHub.
The API token is located on your profile page.
Tried to add the CPT higher on the menu.
function menu_position( $position = 5) {
$this->options["menu_position"] = $position;
}
This works.
It's nice class. However, it could be even better by adding custom capabilities option. Are you planning or give some sort of idea to add it?
hello
I was wondering whether it is possible to set some labels as well so I could have something like "All Books"?
Thanks
Hello
Could you please tell me how I could set up the following code using your class:
register_post_type( 'ta_case',
array(
'labels' => array(
'name' => __( 'Cases', 'ta' ),
'singular_name' => __( 'Case', 'ta' ),
'add_new_item' => __( 'Add new case', 'ta' ),
),
'has_archive' => true,
'hierarchical' => false,
'menu_icon' => 'dashicons-book-alt',
'menu_position' => 50,
'public' => true,
'rewrite' => array( 'slug' => 'case', 'with_front' => false ),
'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'revisions', 'page-attributes', 'genesis-seo', 'genesis-cpt-archives-settings' )
)
);
Thanks
I think it would be great to create a method to set the number of posts per page
I used this class to create a CPT yesterday (great job on this class BTW) and using it with the Advanced Custom Fields plugin is awesome. I wanted to create a CPT that used none of the WP default options...it's possible (since WP 3.5) to do so by passing a boolean false for the 'supports' option. I added a small modification to the code to allow this on line 270:
if ($options === false) return false;
Thought you might consider adding this to the class.
i created two objects $book and $person, and an taxonomy author_type.
$books->register_taxonomy(array(
'taxonomy_name' => 'author_type',
));
$person->register_taxonomy(array(
'taxonomy_name' => 'author_type',
));
but the taxonomy is available for only the last declared object $person but $books did't get the auther_type taxonomy...
I don't know may be i am doing something wrong..
Add the ability to add a column to the existing columns without having to redeclare them all
Warning: Invalid argument supplied for foreach() in /jjgrainger/wp-custom-post-type-class/src/CPT.php on line 865.
I get this warning because it tries to pass a NULL value to a foreach loop. I fixed by typecasting it to an array.
foreach((array) $filters as $tax_slug) {
Was wondering whether there's a way to add a column width - would be nice to have this feature for e.g. true/false columns so they would not take that much space.
i don't know if i'm doing something wrong but i'm not able to translate the name of the post type/taxonomy... not the 'Add New' label but the singular and plural names of the post type/taxonomy (ie: my CPT singular name is "Brands" and i want to translate "Brands").
this is the way i'm registering the post type
// Register Brand custom post type
$brand = new CPT(
// Names
array(
'post_type_name' => 'brand',
'singular' => 'Brand',
'plural' => 'Brands',
'slug' => 'brands'
),
// Options
array(
'has_archive' => true,
'supports' => array('title', 'editor', 'author', 'thumbnail', 'revisions'),
'exclude_from_search' => true,
'map_meta_cap' => true,
'capabilities' => array(
'edit_post' => 'edit_brand',
'edit_posts' => 'edit_brands',
'edit_others_posts' => 'edit_other_brands',
'publish_posts' => 'publish_brands',
'read_post' => 'read_brand',
'read_private_posts' => 'read_private_brands',
'delete_post' => 'delete_brand'
)
)
);
// Icon
$brand->menu_icon('dashicons-star-filled');
// Translation
$brand->set_textdomain('my-text-domain');
i also tried using WordPress translation functions in the names section like so:
array(
'post_type_name' => 'brand',
'singular' => __('Brand'),
'plural' => __('Brands'),
'slug' => 'brands'
),
and with adding a text domain name
array(
'post_type_name' => 'brand',
'singular' => __('Brand', 'my-text-domain'),
'plural' => __('Brands', 'my-text-domain'),
'slug' => 'brands'
),
hope you can help.
thnx.
Hi! I have an issue where I registered a custom post type with two custom taxonomies which works, but when I register a second custom post type that uses those same taxonomies, they only appear under the list view of the second custom post type but not on the actual menu nor in the edit page. Am I missing something?
This is the custom post type with the two original taxonomies:
And here is the second custom post type with the taxonomies only showing up in the admin list view:
My code:
//Litigation Graphics
$litigation = new CPT(
[
'post_type_name' => 'litigation_graphics',
'singular' => 'Litigation Graphic',
'plural' => 'Litigation Graphics',
'slug' => 'litigation-graphics'
],[
'supports' => ['title', 'thumbnail']
]);
$litigation->register_taxonomy(
[
'taxonomy_name' => 'media_type',
'singular' => 'Media Type',
'plural' => 'Media Types',
'slug' => 'media-type'
],[
'hierarchical' => true
]);
$litigation->register_taxonomy(
[
'taxonomy_name' => 'case_type',
'singular' => 'Case Type',
'plural' => 'Case Types',
'slug' => 'case-type'
],[
'hierarchical' => true
]);
//Cases
$cases = new CPT(
[
'post_type_name' => 'cases',
'singular' => 'Case',
'plural' => 'Cases',
'slug' => 'experience'
],[
'supports' => ['title', 'thumbnail', 'editor']
]);
$cases->register_taxonomy(
[
'taxonomy_name' => 'industry',
'singular' => 'Industry',
'plural' => 'Industries',
'slug' => 'industry'
],[
'hierarchical' => true
]);
$cases->register_taxonomy(['taxonomy_name' => 'media_type']);
$cases->register_taxonomy(['taxonomy_name' => 'case_type']);
Thanks for this class, very useful!
One note, in order to ensure taxonomy permalinks always work, it's useful to switch lines 168 and 171, so that the taxonomies are registered first. Bizarre but true, see this post: http://clarknikdelpowell.com/blog/the-right-way-to-do-wordpress-custom-taxonomy-rewrites/
In the current configuration, taxonomy slugs that include a path ("books/genre") instead of just a simple slug ("genre") get created correctly, but result in 404's when you surf to them. Swap so taxonomies are registered first, and they work.
How do I add category for custom post type ? It's similar Posts/Category
Thanks & Regards
Is it a typo that you wrote:
'add_new' => _('Add New'),
I use WPML and could not translate this label. If I change it manually to
'add_new' => __('Add New'),
I can translate the label.
Hi,
First off all, thanks for the code. Registering post types is a breeze but when registering taxonomies, it returns this error:
Fatal error: Call to a member function add_rewrite_tag() on a non-object in .../wp-includes/rewrite.php on line 51
I am using WordPress 3.7.1 by the way.
You have to ensure that
$this->taxonomy_settings
is an array before you iterate with a foreach over it.
I'm getting the following warnings:
PHP Warning: in_array() expects parameter 2 to be array, null given in [theme]/inc/CPT.php on line 591
PHP Warning: in_array() expects parameter 2 to be array, null given in [theme]/inc/CPT.php on line 593
I'm just using this in a pretty basic way, not adding any taxonomies to my CPT. Should this check if there are any taxonomies to work with earlier? (currently does this on :611)
Looks like you didn't add it to the constructor.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.