0

Having issues showing posts based on custom post type's custom taxonomy. I can get ALL the posts to show up in the custom post type but not JUST the taxonomy.

here is how I registered Post Types

function reg_forms() {
  $labels = array(
    'name'               => _x( 'Forms', 'post type general name' ),
    'singular_name'      => _x( 'Form', 'post type singular name' ),
    'add_new'            => _x( 'Add New', 'Form' ),
    'add_new_item'       => __( 'Add New Form' ),
    'edit_item'          => __( 'Edit Form' ),
    'new_item'           => __( 'New Form' ),
    'all_items'          => __( 'All Forms' ),
    'view_item'          => __( 'View Forms' ),
    'search_items'       => __( 'Search Forms' ),
    'not_found'          => __( 'No Forms found' ),
    'not_found_in_trash' => __( 'No Forms found in the Trash' ), 
    'parent_item_colon'  => '',
    'menu_name'          => 'Forms'
  );
  $args = array(
    'labels'        => $labels,
    'description'   => 'Forms for download',
    'public'        => true,
    'menu_position' => 5,
    'supports'      => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'has_archive'   => true,
  );
  register_post_type( 'forms', $args ); 
}
add_action( 'init', 'reg_forms' );

function my_taxonomies_product() {
  $labels = array(
    'name'              => _x( 'Form Categories', 'taxonomy general name' ),
    'singular_name'     => _x( 'Form Category', 'taxonomy singular name' ),
    'search_items'      => __( 'Search Form Categories' ),
    'all_items'         => __( 'All Form Categories' ),
    'parent_item'       => __( 'Parent Form Category' ),
    'parent_item_colon' => __( 'Parent Form Category:' ),
    'edit_item'         => __( 'Edit Form Category' ), 
    'update_item'       => __( 'Update Form Category' ),
    'add_new_item'      => __( 'Add New Form Category' ),
    'new_item_name'     => __( 'New Form Category' ),
    'menu_name'         => __( 'Form Categories' ),
  );
  $args = array(
    'labels' => $labels,
    'hierarchical' => true,
  );
  register_taxonomy( 'form_category', 'forms', $args );
}
add_action( 'init', 'my_taxonomies_product', 0 );

Here is Query.

$the_query = new WP_Query($args = array(
      'post_type' => 'forms',
      'custom_cat' => 'form_category',

      ) );
          // The Loop
    if ( $the_query->have_posts() ) 
    {

            while ( $the_query->have_posts() ) {
              $the_query->the_post();
              echo "<div>";
              echo  get_the_title();
              echo "</div>";
              ;?>

              <?php 
            }
          } 

        else {
          wp_reset_postdata();
        }
0

Try this:

$the_query = new WP_Query($args = array(
      'post_type' => 'forms',
      'tax_query' => array(
        array(
            'taxonomy' => 'form_category',
            'field'    => 'slug'
        ),
      ),

    )
);

btw - Why do you have a 0 in this add_action? Remove it or change it to > 0.

add_action( 'init', 'my_taxonomies_product', 0 );
  • still nothing shows up. I can get all the forms to show up, just not specific categories. – user5288 Dec 14 '14 at 18:17
  • is it cause I have hirearchical set to true, and thus it's pulling all categories? – user5288 Dec 14 '14 at 18:21
0

Turns our I needed to use the Tax Query to target the category... ugh.

<?php
$myposts = get_posts(array(
    'showposts' => -1,
    'post_type' => 'forms',
    'tax_query' => array(
        array(
        'taxonomy' => 'product_category',
        'field' => 'slug',
        'terms' => array('appeals'))
    ))
);

foreach ($myposts as $mypost) {
      echo $mypost->post_title . '<br/>';

}
?>

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.