WordPress.org

Ready to get started?Download WordPress

Forums

'Get the category' not working for custom post type. (7 posts)

  1. Dave J
    Member
    Posted 2 years ago #

    I'm having a tricky problem that I haven't come across before, I've had quite a look on this forum and others for a solution but I'm totally stumped so any pointers would be greatly appreciated.

    In a nutshell, my problem is this -

    I have a custom post type, and on the single pages I originally wanted to have Next / Previous posts that would only navigate through the category that the post is in. For some reason, when I set the argument for "in_same_cat" to 'True', the Next / Previous links didn't appear at all.

    I couldn't figure out why this was happening, so I opted to go for a link simply saying "See all posts in this category" and I intended to use WordPress' get_the_category or posted_in functions - neither of which worked. I'm totally, totally stumped on why these wouldn't work so any help at all, really appreciated.

    Cheers,
    -Dave

    <?php
    /////////////////////////////////////////////////////////////////////////////////////////////////////////
    // CUSTOM POST TYPE - Cases
    /////////////////////////////////////////////////////////////////////////////////////////////////////////
    add_action( 'init', 'create_cases' );
    function create_cases() {
      register_post_type( 'cases',
        array(
        	'labels' => array(
            'name' => __( 'Cases' ),
            'singular_name' => __( 'Cases' ),
    		'add_new' => _x('Add new Case', 'Case item'),
    		'add_new_item' => __('Add new Case'),
    		'edit_item' => __('Edit Case'),
    		'new_item' => __('New Case'),
    		'view_item' => __('View Case'),
    		'search_items' => __('Search Cases'),
    		'not_found' =>  __('Nothing found'),
    		'not_found_in_trash' => __('Nothing found in Trash'),
    		'parent_item_colon' => ''
          ),
          'supports' => array('title','editor','thumbnail'),
          'public' => true,
    	  'exclude_from_search' => false,
          'hierarchical' => true,
          '_builtin' => false,
          'capability_type' => 'post',
          'has_archive' => true,
          'rewrite' => array('slug' => 'case', 'with_front' => false),
          'query_var' => true,
          'permalink_epmask' => EP_PERMALINK,
        )
      );
    }
    
    register_taxonomy("casetaxonomy", array("cases"), array("hierarchical" => true, "label" => "Lawyer", "singular_label" => "Lawyer", "rewrite" => array("slug" => "casetaxonomy")));
    add_action('save_post', 'save_cases');
    function save_cases(){
    	global $post;
    }
    ?>
    function losl_posted_in() {
    	// Retrieves tag list of current post, separated by commas.
    	$tag_list = get_the_tag_list( '', ', ' );
    	if ( $tag_list ) {
    		$posted_in = __( 'This entry was posted in %1$s and tagged %2$s. Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'losl' );
    	} elseif ( is_object_in_taxonomy( get_post_type(), 'category' ) ) {
    		$posted_in = __( 'This entry was posted in %1$s. Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'losl' );
    	} elseif ( is_object_in_taxonomy( get_post_type(), 'casetaxonomy' ) ) {
    		$posted_in = __( 'This entry was posted in %1$s. Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'losl' );
    	} else {
    		$posted_in = __( 'Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>.', 'losl' );
    	}
    	// Prints the string, replacing the placeholders.
    	printf(
    		$posted_in,
    		get_the_category_list( ', ' ),
    		$tag_list,
    		get_permalink(),
    		the_title_attribute( 'echo=0' )
    	);
    }
    endif;
    <div class="columns eight">
        			<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
                    <p class="date"><?php the_time('F jS, Y'); ?></p>
                    <h3><?php the_title(); ?></h3>
                    <?php the_content(); ?>
                    <?php losl_posted_in(); ?>
                    <?php get_the_category() ?>
                    <hr />
    
                    <div class="row">
                        <div class="columns seven">
                            <p><a href="<?php echo home_url( '/' ); ?>case/">See all of our cases</a></p>
                        </div><!--columns seven-->
                        <div class="columns five social-buttons">
        					<?php // share button ?>
        					<?php include(get_template_directory() . "/share-button.php"); ?>
                        </div><!--columns five-->
                    </div><!--row-->
                    <?php endwhile; // end of the loop. ?>
    
        		</div><!--columns eight-->
  2. Dave J
    Member
    Posted 2 years ago #

    Anyone got any ideas on this?

  3. Andrew Bartel
    Member
    Posted 2 years ago #

    If you do a get_categories() to find the actual ID and then do something like get_posts('category' => $id) does it return results? I would do something like that (or go directly to phpmyadmin) to verify that the information is being stored in the database correctly.

  4. Dave J
    Member
    Posted 2 years ago #

    Thanks for the reply Andrew . It's not actually returning results. I'll ask one of the tech guys to check that the DB is storing info correctly.

  5. Esteban
    Member
    Posted 2 years ago #

    Dave J, i was having (i think) the same problem.

    Read this, please:
    http://core.trac.wordpress.org/ticket/21378

  6. Esteban
    Member
    Posted 2 years ago #

    duplicated (nginx server error :/ )

  7. Esteban
    Member
    Posted 2 years ago #

    Well, they say get_the_category it's just for the standard category.
    The Codex warns about:

    This function only returns results from the default "category" taxonomy. For custom taxonomies use get_the_terms.

    Anyways, i fix it by using this new function (put it in your theme's functions.php):

    function get_the_category_bytax( $id = false, $tcat = 'category' ) {
        $categories = get_the_terms( $id, $tcat );
        if ( ! $categories )
            $categories = array();
    
        $categories = array_values( $categories );
    
        foreach ( array_keys( $categories ) as $key ) {
            _make_cat_compat( $categories[$key] );
        }
    
        // Filter name is plural because we return alot of categories (possibly more than #13237) not just one
        return apply_filters( 'get_the_categories', $categories );
    }

    Then call it exactly like get_the_category but passing a second argument: the taxonomy/category you define for you new custom post type.

    So, to get all the category/custom-category info...
    Instead of: $cat = get_the_category($post->ID);
    Use: $cat = get_the_category_bytax($post->ID, 'taxo');
    Where "taxo" it's the custom taxonomy defined for your custom post type.

    That second parameter, if it's added to default get_the_category, is the thing they can put easly in the current function, and still it will work as default for everyone if you don't pass the second param (just let "category" as default).

    Hope they reconsider.

Topic Closed

This topic has been closed to new replies.

About this Topic