0

I am trying to filter custom post type data using ajax. For that, I display some custom taxonomies as select tags.

I've been trying for 6 hours to retrieve custom post types using WP_Query with these custom taxonomy arguments, but I am not able to get the query working.

This is my custom post type:

function cpt_retiro() {
$labels = array(
    'name'                  => _x( 'Retiros', 'Post Type General Name', 'break-breath' ),
    'singular_name'         => _x( 'Retiro', 'Post Type Singular Name', 'break-breath' ),
    'menu_name'             => __( 'Retiros', 'break-breath' ),
    'name_admin_bar'        => __( 'Retiros', 'break-breath' ),
    'archives'              => __( 'Archivos del retiro', 'break-breath' ),
    'attributes'            => __( 'Atributos del retiro', 'break-breath' ),
    'parent_item_colon'     => __( 'Elemento padre', 'break-breath' ),
    'all_items'             => __( 'Todos los retiros', 'break-breath' ),
    'add_new_item'          => __( 'Añadir nuevo retiro', 'break-breath' ),
    'add_new'               => __( 'Añadir nuevo', 'break-breath' ),
    'new_item'              => __( 'Nuevo retiro', 'break-breath' ),
    'edit_item'             => __( 'Editar retiro', 'break-breath' ),
    'update_item'           => __( 'Actualizar retiro', 'break-breath' ),
    'view_item'             => __( 'Ver retiro', 'break-breath' ),
    'view_items'            => __( 'Ver retiros', 'break-breath' ),
    'search_items'          => __( 'Buscar retiro', 'break-breath' ),
    'not_found'             => __( 'No se han encontrado retiros', 'break-breath' ),
    'not_found_in_trash'    => __( 'No se han encontrado retiros en la papelera', 'break-breath' ),
    'featured_image'        => __( 'Imagen destacada', 'break-breath' ),
    'set_featured_image'    => __( 'Seleccionar imagen destacada', 'break-breath' ),
    'remove_featured_image' => __( 'Borrar imagen destacada', 'break-breath' ),
    'use_featured_image'    => __( 'Establecer como imagen destacada', 'break-breath' ),
    'insert_into_item'      => __( 'Insertar en retiro', 'break-breath' ),
    'uploaded_to_this_item' => __( 'Subido a este retiro', 'break-breath' ),
    'items_list'            => __( 'Listado de retiros', 'break-breath' ),
    'items_list_navigation' => __( 'Navegación entre retiros', 'break-breath' ),
    'filter_items_list'     => __( 'Filtrar retiros', 'break-breath' ),
);
$args = array(
    'label'                 => __( 'Retiro', 'break-breath' ),
    'description'           => __( 'Retiros', 'break-breath' ),
    'labels'                => $labels,
    'supports'              => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
    'taxonomies'            => array( 'category', 'post_tag', 'provincia', 'ciudad' ),
    'hierarchical'          => true,
    'show_ui'               => true,
    'show_in_menu'          => true,
    'menu_position'         => 5,
    'menu_icon'             => 'dashicons-location-alt',
    'show_in_admin_bar'     => true,
    'show_in_nav_menus'     => true,
    'can_export'            => true,
    'has_archive'           => true,
    'exclude_from_search'   => false,
    'publicly_queryable'    => true,
    'capability_type'       => 'page',
);
register_post_type( 'retiro', $args );}

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

This is one of my registered taxonomies:

function taxonomia_provincia_retiro() {
$labels = array(
    'name'                       => _x( 'Provincias', 'Taxonomy General Name', 'break-breath' ),
    'singular_name'              => _x( 'Provincia', 'Taxonomy Singular Name', 'break-breath' ),
    'menu_name'                  => __( 'Provincias', 'break-breath' ),
    'all_items'                  => __( 'Todas las provincias', 'break-breath' ),
    'new_item_name'              => __( 'Añadir nuevo nombre de provincia', 'break-breath' ),
    'add_new_item'               => __( 'Añadir nueva provincia', 'break-breath' ),
    'edit_item'                  => __( 'Editar provincia', 'break-breath' ),
    'update_item'                => __( 'Actualizar provincia', 'break-breath' ),
    'view_item'                  => __( 'Ver provincia', 'break-breath' ),
    'separate_items_with_commas' => __( 'Separar provincias por comas', 'break-breath' ),
    'add_or_remove_items'        => __( 'Añadir o borrar provincias', 'break-breath' ),
    'choose_from_most_used'      => __( 'Elegir provincia entre las más usadas', 'break-breath' ),
    'popular_items'              => __( 'Provincias populares', 'break-breath' ),
    'search_items'               => __( 'Buscar provincias', 'break-breath' ),
    'not_found'                  => __( 'No se han encontrado provincias', 'break-breath' ),
    'no_terms'                   => __( 'No existen provincias', 'break-breath' ),
    'items_list'                 => __( 'Listado de provincias', 'break-breath' ),
    'items_list_navigation'      => __( 'Navegación entre provincias', 'break-breath' ),
);
$args = array(
    'labels'                     => $labels,
    'hierarchical'               => true,
    'public'                     => true,
    'show_ui'                    => true,
    'show_admin_column'          => true,
    'show_in_nav_menus'          => true,
    'show_tagcloud'              => true,
    'query_var'                  => true
);
register_taxonomy( 'provincia', array( 'retiro' ), $args );}

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

So, the final result is:

Final CPT

Another example: Example

If i do this, It works fine:

$args = array('post_type' => 'retiro');
$query = new WP_Query($args);

But, when I try to do this as the codex says, it does not work:

$args = array(
'post_type' => 'retiro',
'tax_query' => array(
    array(
        'taxonomy' => 'provincia'
    ),
),);

$query = new WP_Query( $args );

I am getting crazy, what is wrong with this code?? Thanks in advance for helping me! Have a nice day!

  • 1
    You need to provide the terms parameter, which is the taxonomy term(s). E.g. 'terms' => array( 1, 2 ) to get posts in the terms where the term ID is either 1 or 2. Check (again) the examples here. – Sally CJ Mar 13 '19 at 14:39
  • Thanks!! I tried and now its working, but if I want to display all posts from a custom taxonomy, do I need to pass all taxonomy terms? Or is there an easiest way? – adeva 2006 Mar 13 '19 at 15:23
  • Try with 'terms' => get_terms( 'taxonomy=provincia&fields=ids' ), 'field' => 'term_id'. (The field parameter is optional though, when the terms is a term IDs array/list.) For other taxonomies, just replace the provincia with the taxonomy slug. – Sally CJ Mar 13 '19 at 19:45
3

You need to pass the field and terms to get this working the right way:

'tax_query' => array(
    'relation' => 'AND',
    array(
        'taxonomy' => 'provincia',
        'field'    => 'slug',
        'terms'    => array( 'term1', 'term2' ),
    ),
),

https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

  • Thanks!! You make my day!! – adeva 2006 Mar 13 '19 at 15:22

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.