1

I'm trying to query a custom post type by its category slug, but its not working at all

my custom post type definitions:

add_action( 'init', 'custom_post_albuns' );
 
// The custom function to register a movie post type
function custom_post_albuns() {
 
  // Set the labels, this variable is used in the $args array
  $labels = array(
    'name'               => __( 'Albuns' ),
    'singular_name'      => __( 'Album' ),
    'add_new'            => __( 'Add New Album' ),
    'add_new_item'       => __( 'Add New Album' ),
    'edit_item'          => __( 'Edit Album' ),
    'new_item'           => __( 'New Album' ),
    'all_items'          => __( 'All Albuns' ),
    'view_item'          => __( 'View Album' ),
    'search_items'       => __( 'Search Albuns' ),
    'featured_image'     => 'Featured Image',
    'set_featured_image' => 'Add Featured Image'
  );
 
  // The arguments for our post type, to be entered as parameter 2 of register_post_type()
  $args = array(
    'labels'            => $labels,
    'description'       => 'm6 Records Albuns',
    'public'            => true,
    'menu_position'     => 6,
    'supports'          => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments', 'custom-fields' ),
    'has_archive'       => true,
    'show_in_admin_bar' => true,
    'show_in_nav_menus' => true,
    'has_archive'       => true,
    'menu_icon'         => 'dashicons-album',
    'query_var'         => 'album'
  );
 
  // Call the actual WordPress function
  // Parameter 1 is a name for the post type
  // Parameter 2 is the $args array
  register_post_type( 'album', $args);
}

add_action( 'init', 'create_album_taxonomies', 0 );

function create_album_taxonomies() {
    $labels = array(
        'name'              => _x( 'Categories', 'taxonomy general name' ),
        'singular_name'     => _x( 'Category', 'taxonomy singular name' ),
        'search_items'      => __( 'Search Categories' ),
        'all_items'         => __( 'All Categories' ),
        'parent_item'       => __( 'Parent Category' ),
        'parent_item_colon' => __( 'Parent Category:' ),
        'edit_item'         => __( 'Edit Category' ),
        'update_item'       => __( 'Update Category' ),
        'add_new_item'      => __( 'Add New Category' ),
        'new_item_name'     => __( 'New Category Name' ),
        'menu_name'         => __( 'Categories' ),
    );

    $args = array(
        'hierarchical'      => true, // Set this to 'false' for non-hierarchical taxonomy (like tags)
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'albuns' ),
    );

    register_taxonomy( 'albuns_categories', array( 'album' ), $args );
}

and my query:

$argsPost = array(
                    'post_type'=>'album', 
                    'orderby'=>'date',
                    'order'   => 'DESC',
                    'posts_per_page'=>'4',
                    'tax_query' => array(
                        array(
                            'taxonomy' => 'albuns_categories',
                            'field' => 'slug', 
                            'terms' => array( 'ANYTERM' ) 
                        )
                    )
                  );

and I can't bring a list of albuns (if I remove the tax_query part, it returns to me all the "albuns" custom post type)

Thanks!!

2
  • 1
    How are you getting the term slug into your tax query? 'ANYTERM' won't return anything. Can you also include your query, just in case, you've only included your arguments. For the sake of argument, trying removing the array() from terms and hard code one of the slugs in there and see if that returns anything. Commented Mar 10, 2021 at 4:26
  • anyterm was an example, i have the current category slugs: "mercedez", "conspira", "alex", and it wont return Commented Mar 10, 2021 at 11:49

1 Answer 1

1

I've got an answer that worked perfeclty for me in another forum, so I'll post here if sometime anyone gets a similar problem

$argsPost = array(
    'post_type'=>'album', 
    'orderby'=>'date',
    'order'   => 'DESC',
    'posts_per_page'=>'4',
    'tax_query' => array(
        array(
            'taxonomy' => 'albuns_categories',
            'field' => 'slug', 
            'terms' => array( 'ANYTERM' ),
            'operator' => 'IN'
        )
    )
);

=D

1
  • 1
    Glad you found it; remember to accept your own answer so other users know it works. 😊 Commented Mar 10, 2021 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.