Trouble displaying list of a custom post type.

I'm attempting to list the posts of a custom post type, in this case the galleries in the Touchfolio theme (https://github.com/dimsemenov/touchfolio)

I have pasted the function that adds the post type below. I tried doing:

wp_list_pages($args); and passing post_type => 'ds_gallery'
and
post_type => 'gallery'
as well as:
wp_list_categories($args);
and passing
taxonomy => 'gallery'
taxonomy => 'album'

No matter what I tried it returned false:slight_frown:
What am I missing here??

<br />
public function register_ds_gallery_post() {</p>
<p>	    	 $gallery_cat_labels = array(<br />
				'name' => _x( 'Gallery_Categories', 'dsframework' ),<br />
				'singular_name' => _x( 'Gallery Category', 'dsframework' ),<br />
				'search_items' => _x( 'Search Gallery Categories', 'dsframework' ),<br />
				'popular_items' => _x( 'Popular Gallery Categories', 'dsframework' ),<br />
				'all_items' => _x( 'All Gallery Categories', 'dsframework' ),<br />
				'parent_item' => _x( 'Parent Gallery Category', 'dsframework' ),<br />
				'parent_item_colon' => _x( 'Parent Gallery Category:', 'dsframework' ),<br />
				'edit_item' => _x( 'Edit Gallery Category', 'dsframework' ),<br />
				'update_item' => _x( 'Update Gallery Category', 'dsframework' ),<br />
				'add_new_item' => _x( 'Add New Gallery Category', 'dsframework' ),<br />
				'new_item_name' => _x( 'New Gallery Category Name', 'dsframework' ),<br />
				'separate_items_with_commas' => _x( 'Separate gallery categories with commas', 'dsframework' ),<br />
				'add_or_remove_items' => _x( 'Add or remove gallery categories', 'dsframework' ),<br />
				'choose_from_most_used' => _x( 'Choose from the most used gallery categories', 'dsframework' ),<br />
				'menu_name' => _x( 'Gallery Categories', 'dsframework' ),<br />
		    );</p>
<p>			$args = array(<br />
			    'hierarchical' => true,<br />
				'labels' => $gallery_cat_labels,<br />
			    'show_ui' => true,<br />
			    'query_var' => true,<br />
			    'public' => true,<br />
			    'rewrite' => array( 'slug' => 'gallery-category' )<br />
			);</p>
<p>			register_taxonomy( 'ds-gallery-category', 'ds-gallery', $args );</p>
<p>			$labels = array(<br />
				'name' => __('Gallery', 'dsframework'),<br />
				'singular_name' => __('Album', 'dsframework'),<br />
				'add_new' => __('Add New Album', 'dsframework'),<br />
				'add_new_item' => __('Add New Album', 'dsframework'),<br />
				'edit_item' => __('Edit Album', 'dsframework'),<br />
				'view_item' => __('View Album'),<br />
				'new_item' => __('New Album', 'dsframework'),<br />
				'search_items' => __('Search Albums', 'dsframework'),<br />
				'not_found' =>  __('Albums not found', 'dsframework'),<br />
				'not_found_in_trash' => __('Albums not found in trash', 'dsframework'),<br />
				'parent_item_colon' => ''<br />
			);</p>
<p>			$args = array(<br />
				'labels' => $labels,<br />
				'public' => true,<br />
				'publicly_queryable' => true,<br />
				'show_ui' => true,</p>
<p>				'rewrite' => array(<br />
					'slug' => 'gallery'<br />
				),<br />
				'query_var' => true,<br />
				'capability_type' => 'post',<br />
				'hierarchical' => false,<br />
				'menu_position' => 5,<br />
				'supports' => array('title', 'editor', 'thumbnail', 'excerpt'),<br />
				'menu_icon' => get_stylesheet_directory_uri() . '/img/gallery-menu-icon.png'<br />
			  ); </p>
<p>			register_post_type( 'ds-gallery' , $args);<br />
			flush_rewrite_rules();<br />
		}<br />
  • aecnu

    Greetings josh_pollock,

    Thank you for bringing this to our attention and custom coding is beyond the scope of the support that we provide here though we do try to help if we can.

    With that said I will go ahead and see if I can get a lead developer/coder in here to perhaps answer this item for us.

    Though this may take a bit longer then a normal ticket, I will try to get one of them in here asap.

    Cheers, Joe

  • Ignacio

    Hi Josh.

    First of all, check the CPT slug. You're trying to list posts using post_type = 'ds_gallery' but, look, your CPT slug is actually 'ds-gallery'. So, well, we have to change that first, but...

    You still will get no results (null result), why? Beacuse wp_list_pages() will only return results IF you CPT is a hierarchical one (http://codex.wordpress.org/Function_Reference/wp_list_pages#List_members_of_a_custom_post_type) but here's your definition:

    'capability_type' => 'post',
    'hierarchical' => false,
    'menu_position' => 5,
    'supports' => array('title', 'editor', 'thumbnail', 'excerpt'),

    Change that false by true and you'll see a list of galleries but you'll probably don't want a hierarchical CPT. Then we need another different approach using get_posts(). Here's a small example:

    <?php
    $args =  array(
       'numberposts'		=>	0,
       'post_type'			=> 'ds-gallery'
    );
    
    $gallery_posts = get_posts( $args );
    
    ?>
    
    <?php foreach ( $gallery_posts as $post ) : setup_postdata( $post ); ?>
       <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endforeach; ?>

    numberposts must be equal to 0 if you want to see all the posts.

    And finally here's the code to list the categories:

    $args = array(
      'taxonomy' => 'ds-gallery-category'
    );
    wp_list_categories( $args );

    And one more recommendation: Do not flush the rewrite rules every time you create the CPT. You should only flush them when activating the theme/plugin where you inserted that code:

    http://codex.wordpress.org/Function_Reference/flush_rewrite_rules

    Basically, it's an expensive operation and with that code you're flushing the rules everytime the site is loaded.

    I hope this was useful.

    Regards.

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.