0

I don't want to add the category support for the posts of a certain custom post type but to categorize the post types itself. How is that possible?

Story behind:

In my theme I handle different post types differently. But certain post types are being processed in the same way, so a categorisation of the post types makes sense.

As the following is no modular coding:

if ( get_post_type() === 'movies' || get_post_type() === 'apples' || get_post_type() === 'sausages' ) ) : //…

I want something more like this:

if ( in_category( 'food', get_post_type() ) : //…

One attempt would probably be to add a custom variable to register_post_type(). Of course I want to achieve this without modifying the WordPress core.

  • Do you need this "classification" only on display? – s_ha_dum Nov 3 '14 at 21:42
  • What do you mean by 'only on display'? I don't really need an admin page to configure this functionality (would be nice of course, but would be another question). It would fulfill to add a class/category in form of an argument/array item within register_post_type(). – luke Nov 3 '14 at 21:49
  • 'only on display' == "when the content is displayed to users on the front end". It seems a bit to me like you are overcomplicating things. Something like the answers already given, while not strictly answering your question, seem like the best way to go to me given the information provided about the problem. – s_ha_dum Nov 3 '14 at 21:51
  • Yeah, I need this only on the front end. For the example given this is of course not really needed, but a more complex data structure would perhaps have the need of something like a classification of post types. Perhaps the question's title would make more (general) sense if it would be How to add custom data to custom post type – whatever one can do with it later. The idea of @Howdy_McGee is good and I will use the description field if no other solution will be found, but the general functionality would probably be interesting for other users, too. – luke Nov 3 '14 at 22:03
2

Thanks guys, I found a solution:

It's as easy as adding the post_type_category element to the arguments object within register_post_type():

$args = array(
    'label'               => 'sausages',
    'description'         => 'Sausages',
    'labels'              => $labels,
    'post_type_category'  => 'food',
    'supports'            => array( 'title', 'category' ),
    'hierarchical'        => false,
    /* and so on */
);
register_post_type( 'sausage', $args );

And then add this to your functions.php:

function is_post_type_in_cat ( $category = null , $post_type = null ) {

    if(!$post_type) $post_type = get_post_type();

    $post_type_object = get_post_type_object( $post_type  );

    $arr = isset($post_type_object->post_type_category) ? $post_type_object->post_type_category : null;

    if( !$arr ) return false;

    if ( !is_array($arr) )

        $arr = array($arr);

    return in_array($category, $arr);

}

You can then check in your theme with the following code:

if( is_post_type_in_cat( 'food' ) ) :
  //do something
else:
  //do something
endif;

You can also not only for the current but for a specific post type:

if( is_post_type_in_cat( 'food', 'sausage' ) ) :
  //do something
else:
  //do something
endif;

Also an array of categories can be given as a parameter:

if( is_post_type_in_cat( array('food', 'animal', 'dairy', 'cheese'), 'gouda' ) ) : // and so on

Easier than I thought. Thanks to all contributors! Perhaps in the future I'm gonna build a lightweight plugin with a GUI for this if this will not be implemented in the major CPT-Plugins or even the WP core.

  • I upvoted you, but I would caution you about this code. I don't know if this is intentional functionality or not. Changes to Core code could break it. – s_ha_dum Nov 4 '14 at 17:29
  • Like every plugin on every WordPress update it will have to be rechecked and perhaps some parts of the code will have to be rewritten. If this happens I'm gonna update the code here on SO too. Good point still! – luke Nov 4 '14 at 20:56
  • Yes, but some code is more prone to those problems than other code. – s_ha_dum Nov 4 '14 at 20:58
  • Do you have a suggestions how to optimize this code? As the post type object is not stored in the data base but will be regenerated every time WordPress loads I think (but I don't know) this code will rather be uncritical. Why do you think this code would be more prone? – luke Nov 4 '14 at 21:05
  • 1
    register_post_type could easily put limits on the array elements-- lock them down, so to speak. – s_ha_dum Nov 4 '14 at 21:09
1

I'm assuming you're not using a plugin to register your post types and doing them yourself. WordPress doesn't have an easy way to group or categorize post types together like what you're asking for. What I would suggest is to use built in attributes in a custom way. For example, when registering your post type there is a field called description. I don't see post type descriptions being used much so if you're not using it, you could add your category in there and run conditionals on the description similar to how you would a category - you just need to assign it on creation / registering.

For 'apples' and 'sausages' my post type description could be 'food'. and I could check it by doing something like this:

$post_type = get_post_type();

if( ! empty( $post_type ) ) {
    $typeObj = get_post_type_object( $post_type );

    if( ! empty( $typeObj ) && 'food' == $typeObj->description ) {
        // Do something with the food post type category
    }
}

Another thing if you really want to keep track of it is keep a global associate array of

array( 'post_type_name' => 'post_type_category' )

The problem with that is that you would need to update the global array each time you create a new post type.

  • Using the description field is a good idea. Thanks! Though it is a workaround, it would be a good solution for the future to add custom data to custom post types. Is there a way for this? One could also build a plugin of this for other users. – luke Nov 3 '14 at 21:55
  • The array is also a solution I thought about, but I want to avoid this extra effort to keep it as clean/modular as possible. – luke Nov 3 '14 at 21:56
  • Maybe somebody will make a plugin in the future to do this ( or maybe WordPress will build it in ) but this scenario seems uncommon for most installs so I'm not sure how beneficial a feature like this would be. The description route seems like your best bet. I'd try and avoid global arrays when I can. That being said if you do use post type archives @Welchers answer is probably best since every post type has to have a name and you can group them as you go. Some people do use Secondary Queries though so it depends on your set up. – Howdy_McGee Nov 3 '14 at 22:03
  • Thanks @Howdy_McGee, the get_post_type_object() function led me to the solution, see my answer. – luke Nov 3 '14 at 22:36
1

I am make some assumptions here on where you are needing this but you could use is_post_type_archive() to check for archive pages and is_singular() for individual single posts both take either an string or array of post types.

$post_types_group_one = array('movies', 'apples', 'sausages' );
$post_types_group_two = array('movies', 'apples', 'sausages', 'pancakes' );

if( is_post_type_archive( $post_types_group_one ) {
    //do something
}

if( is_singular( $post_types_group_two  ) ) {
    //do something
}

Hope it helps!

  • Thanks Welcher, but I want to avoid naming the post types' names every time to keep it modular. – luke Nov 3 '14 at 21:57
  • Keep the post type groups in an array elsewhere and just refer to them as needed. – Welcher Nov 3 '14 at 22:00

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.