• I having some difficulty figuring out how to query a custom post type in a specific (custom) category..

    So far I have the following code in my functions.php

    function post_type_discog() {
    
     register_post_type('discography',
      array(
        'labels' => array(
        'name' => __( 'Discography' ),
        'singular_name' => __( 'Discography' ),
        'add_new' => __( 'Add New' ),
        'add_new_item' => __( 'Add New Discography' ),
        'edit' => __( 'Edit' ),
        'edit_item' => __( 'Edit Discography' ),
        'new_item' => __( 'New Discography' ),
        'view' => __( 'View Discography' ),
        'view_item' => __( 'View Discography' ),
        'search_items' => __( 'Search Discographys' ),
        'not_found' => __( 'No Discographys found' ),
        'not_found_in_trash' => __( 'No Discographys found in Trash' ),
        'parent' => __( 'Parent Discography' ),
       ),
          'public' => true,
          'show_ui' => true,
       'exclude_from_search' => true,
       'hierarchical' => true,
       'supports' => array( 'title', 'editor', 'thumbnail' ),
       'query_var' => true
       )
       );
     }
     add_action('init', 'post_type_discog');
    
     add_action( 'init', 'create_discog_taxonomies', 0 );
    
     function create_discog_taxonomies()
     {
       // Add new taxonomy, make it hierarchical (like categories)
       $labels = array(
         'name' => _x( 'Recordings', 'taxonomy general name' ),
         'singular_name' => _x( 'Recording', 'taxonomy singular name' ),
         'search_items' =>  __( 'Search Recordings' ),
         'popular_items' => __( 'Popular Recordings' ),
         'all_items' => __( 'All Recordings' ),
         'parent_item' => __( 'Parent Recording' ),
         'parent_item_colon' => __( 'Parent Recording:' ),
         'edit_item' => __( 'Edit Recording' ),
         'update_item' => __( 'Update Recording' ),
         'add_new_item' => __( 'Add New Recording' ),
         'new_item_name' => __( 'New Recording Name' ),
       );
       register_taxonomy('recordings',array('discography'), array(
         'hierarchical' => true,
         'labels' => $labels,
         'show_ui' => true,
         'query_var' => true,
         'rewrite' => array( 'slug' => 'recordings' ),
       ));
     }

    Which works great (on the surface at least!). I can create a custom post type (which I can retrieve with WP_Query like so:

    <?php
        $new = new WP_Query('post_type=discography');
        while ($new->have_posts()) : $new->the_post();
         the_content();
        endwhile;
    ?>

    But if I add a category I can’t seem to filter my results as I normally would, by adding:

    category_name=”my-category”

    to the WP_Query

    $new = new WP_Query('post_type=discography&category_name=my-category');

    This won’t display anything! I’ve tried renaming ‘category_name’ to ‘recordings_name’, but then it just displays all the entries in that custom post type.

    Help!

Viewing 3 replies - 31 through 33 (of 33 total)
  • You guys are awesome thank you everyone!

    I’m also using the code mentioned by Ryan Riatno, and I can get it to list all the custom taxonomies, but it will list all the custom posts under each taxonomy, rather than just listing the 1 or 2 custom posts that actually belong under the custom tax.

    For example…
    Tax X
    Custom Post 1
    Custom Post 2
    Custom Post 3
    Tax Y
    Custom Post 1
    Custom Post 2
    Custom Post 3
    Tax X
    Custom Post 1
    Custom Post 2
    Custom Post 3
    Tax Z
    Custom Post 1
    Custom Post 2
    Custom Post 3

    Whereas what I want is…

    Tax X
    Custom Post 1
    Tax Y
    Custom Post 2
    Tax Z
    Custom Post 3

    Here’s my code…

    [Code moderated as per the Forum Rules. Please use the pastebin]

    I’m willing to try anything. I’ve tried about 10 different ways to write this, but they either give me the same results, or no results at all.

    Sorry about breaking the Forum rules…

    Here’s a link to the code I posted @ pastebin: https://pastebin.com/0ttqaYjZ

Viewing 3 replies - 31 through 33 (of 33 total)
  • The topic ‘custom posts type with custom taxonomy wp_query’ is closed to new replies.