• tturner

    (@tturner)


    I’m hoping that by now using custom ‘post_type'(s) has become a little more common.

    Does anyone have any “best practices” when using custom post_types?

    I noticed ‘custom’ is being added to the passable options for ‘post_type’ in the get_posts() function for WP Version 2.9. Until the version becomes available (stable) is there a way to get posts with a custom type that is “better” then just doing a raw db query?

    Are there any functions/hooks that allow the passing of custom ‘post_type'(s)?

    If I don’t find any preexisting methods for cleanly handling the data, I’ll code some up and post them here.

Viewing 9 replies - 1 through 9 (of 9 total)
  • I very much need to know how to create a new post type in my blog. I found something about specifying post_type and tried it, but I don’t seem to know how to do it correctly. Any help would be deeply appreciated!

    Yes, there seems to be a dearth of documentation on the new custom post types. I’d like to know how to add a new post type and then not only how to select just that post type but also how to choose to exclude those posts from other functions, such as RSS feeds, site searches, home page, archives, etc.

    I’m thinking I might want to write a very simple plugin for discussion forums that use the standard WP tables. The benefits would be that the forums could automatically show in site navigation, standard widgets for posts/pages, site searches; automatic comment and pingback capability; etc.

    I did find just one reference to this in wp-includes/posts.php there is a call to a function register_post_type(). However, doing a quick file search, I can’t find where that function itself is defined. Of course many standard WP functions have always allowed a post_type parameter, so I assume they have been rewritten to validate posts types against those that are officially registered with register_post_type() so not much change is needed in selecting the custom post types.

    I have also read that the “user interface” for custom post types is not coming until WP v3.0. I’m not really sure what that means, although I guess it probably means there will be more specialized functions for selecting, displaying, updating custom post types just to make things easier.

    At the least you could use post_type=your_type_here with a query, but until a plugin and/or Version 3.0 provides UI access not sure how much you can do.

    Related:
    Function_Reference/register_post_type

    Michael: Is there a filter or action hook that can be used to set post_type=your_type_here in the standard query, or do we have to construct a complete fresh query to make use of custom types? I have been reading the query.php source but it’s making me go blind :-S

    Thanks.

    Not sure.

    I’d guess that $query_posts('post_type=your_type_here'); would retrieve the data.

    Justin Tadlock points out that that you can add post types into the query by hooking into the pre_get_posts filter hook and running something like $query->set( ‘post_type’, array( ‘post’, ‘page’, ‘classified’, ‘attachment’ ) );

    So to include posts of type classified mixed with ‘normal’ posts, I use this:

    function my_get_posts( $query ) {
    
    	if ( is_home() || is_single() )
    		$query->set( 'post_type', array( 'post', 'classified' ) );
    
    	return $query;
    }
    
    add_filter( 'pre_get_posts', 'my_get_posts' );

    So far I’m only assuming that this will still work when I build in my own template & query vars.

    I have done more analysis on the WP 2.9 code and researched 3.0 functionality in the WP tracking system. In 2.9, there is only the ability to declare a custom post type and specify that post type in a custom query.

    In 3.0, declaring a custom post type will automatically create a link and post maintenance screen in the admin section for them. It will also let you specify whether the post type is publically queryable, whether it shows in search results by default, etc. 3.0 also allows you to create custom taxonomies (categories, tags) for the custom post types.

    So this makes sense with the general comments I’ve seen on these forums where people are saying they are going to wait until WP 3.0 when the “UI” (user interface) is built out. With 2.9, everything has to be hand-coded. With 3.0, some simple custom post type and taxonomy declarations will make these things immediately useful without a lot of custom coding.

    I have used “custom type” over and over again in wordpress 2.9.2 . I simply make sure the database has that post type and I query it the same way I would query a regular post/page.

    That is, I created a script to insert posts and specify post_type to be “personal” which is a custom post type. Then I use a regular query as:

    $recent = new WP_Query(“post_type=personal&author=”.$user_id.””);

    It works perfectly. Let me know if you need help with that.

    ishimwe

    I am very interested in that. I’m a little green on this stuff. Can you give a little more detail about where you place that script code to create the new type? And what it looks like?

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Using Custom ‘post_type’’ is closed to new replies.