I am trying to get out a custom posttypes that belongs to a specific taxonomy. I don't get it sorted out on taxonomy, it takes every custom post. I use this code
global $post;
$terms = get_terms('resmal');
foreach ($terms as $term) :
echo get_term_link($term);
//Define the query
$args = array(
'post_type' => 'resor',
'tax-query' => array(
array(
'taxonomy' => 'resmal',
'field' => 'slug',
'terms' => 'Italien'
)
)
);
$query = new WP_Query( $args );
// Start the Loop
if( $query->have_posts() ):
while ( $query->have_posts() ) :
$query->the_post();
echo get_the_title();
endwhile;
endif;
wp_reset_query();
wp_reset_postdata();
endforeach;
The custom posttype
function base_register_resor_post_type() {
$labels = array(
'name' => __( 'Våra Resor'),
'singular_name' => __( 'Resa' ),
'add_new' => __('Skapa ny'),
'add_new_item' => __('Skapa ny Resa'),
'edit_item' => __('Redigera Resa'),
'new_item' => __('Ny Resa'),
'view_item' => __('Visa Resa'),
'search_items' => __('Sök Resa'),
'not_found' => __('Hittade inget'),
'not_found_in_trash' => __('Papperskorgen är tom!')
);
$args = array(
'labels' => $labels,
'public' => true,
'has_archive' => 'resor',
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => array('slug' => 'resa'),
'taxonomies' => array('resmal'),
'capability_type' => 'post',
'supports' => array('title','editor','thumbnail','excerpt','revisions'),
'show_in_nav_menus' => true,
'hierarchical' => true
);
register_post_type('resor', $args);
}
add_action('init', 'base_register_resor_post_type');
The taxonomy
function base_register_destination_taxonomy() {
$labels = array(
'name' => 'Resmål',
'singular_name' => 'Resmål',
'search_items' => 'Sök Resmål',
'all_items' => 'Alla Resmål',
'parent_item' => 'Parent Location',
'parent_item_colon' => 'Parent Location:',
'edit_item' => 'Ändra Resmål',
'update_item' => 'Updatera Resmål',
'add_new_item' => 'Lägg till nytt Resmål',
'new_item_name' => 'Nytt Resmål namn',
'menu_name' => 'Resmål'
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'has_archive' => true,
'rewrite' => array( 'slug' => 'resmal' ),
'public' => true
);
register_taxonomy('resmal', array('resor'), $args);
}
add_action('init', 'base_register_destination_taxonomy');
I am trying to get out a custom posttypes that belongs to a specific taxonomy. I don't get it sorted out on taxonomy, it takes every custom post. I use this code
global $post;
$terms = get_terms('resmal');
foreach ($terms as $term) :
echo get_term_link($term);
//Define the query
$args = array(
'post_type' => 'resor',
'tax-query' => array(
array(
'taxonomy' => 'resmal',
'field' => 'slug',
'terms' => 'Italien'
)
)
);
$query = new WP_Query( $args );
// Start the Loop
if( $query->have_posts() ):
while ( $query->have_posts() ) :
$query->the_post();
echo get_the_title();
endwhile;
endif;
wp_reset_query();
wp_reset_postdata();
endforeach;
The custom posttype
function base_register_resor_post_type() {
$labels = array(
'name' => __( 'Våra Resor'),
'singular_name' => __( 'Resa' ),
'add_new' => __('Skapa ny'),
'add_new_item' => __('Skapa ny Resa'),
'edit_item' => __('Redigera Resa'),
'new_item' => __('Ny Resa'),
'view_item' => __('Visa Resa'),
'search_items' => __('Sök Resa'),
'not_found' => __('Hittade inget'),
'not_found_in_trash' => __('Papperskorgen är tom!')
);
$args = array(
'labels' => $labels,
'public' => true,
'has_archive' => 'resor',
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => array('slug' => 'resa'),
'taxonomies' => array('resmal'),
'capability_type' => 'post',
'supports' => array('title','editor','thumbnail','excerpt','revisions'),
'show_in_nav_menus' => true,
'hierarchical' => true
);
register_post_type('resor', $args);
}
add_action('init', 'base_register_resor_post_type');
The taxonomy
function base_register_destination_taxonomy() {
$labels = array(
'name' => 'Resmål',
'singular_name' => 'Resmål',
'search_items' => 'Sök Resmål',
'all_items' => 'Alla Resmål',
'parent_item' => 'Parent Location',
'parent_item_colon' => 'Parent Location:',
'edit_item' => 'Ändra Resmål',
'update_item' => 'Updatera Resmål',
'add_new_item' => 'Lägg till nytt Resmål',
'new_item_name' => 'Nytt Resmål namn',
'menu_name' => 'Resmål'
);
$args = array(
'hierarchical' => true,
'labels' => $labels,
'has_archive' => true,
'rewrite' => array( 'slug' => 'resmal' ),
'public' => true
);
register_taxonomy('resmal', array('resor'), $args);
}
add_action('init', 'base_register_destination_taxonomy');
Share
Improve this question
asked Jan 31, 2020 at 8:16
Mats GustavssonMats Gustavsson
54 bronze badges
1 Answer
Reset to default 0You seem to have a typo in the first snippet that is
'tax-query' => array(
instead of
'tax_query' => array(