Get all post from custom post type in custom taxonomy

I am trying to display a custom post type that has custom taxonomy, but I am not having any luck. Nothing is showing up. I appreciate any help.

Post type = banner

Custom Taxonomy = country

The ‘country’ I want to display = Algeria

My code :-

<?php
$slide = new wp_query( array ( 'post_type' => 'banner' , 'taxonomy' => 'country', 'term' => 'algeria', 
                            'meta_query' => array( 'relation' => 'AND',
                                                                        array('key' => 'page_ads','value' => 'home'),
                                                                        array('key' => 'position_ads','value' => 'top'))
                            ));

if ( $slide->have_posts() ) : while ( $slide->have_posts() ) : $slide->the_post(); 
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' ); 
?>   
<div class="item"><a href="<?php echo get_option("siteurl")?>/order-ads">    
<img src="<?php echo $image[0]; ?>" width="1350" height="515">   
                                </a>
                                </div> 
<?php
                            endwhile; endif;
                            wp_reset_query();
                            echo "</div><br>";
     }
                            ?>

Register taxonomy

// hook into the init action and call create_book_taxonomies when it fires
add_action( 'init', 'create_banner_taxonomies', 0 );

// create two taxonomies, genres and writers for the post type "book"
function create_banner_taxonomies() {
    // Add new taxonomy, make it hierarchical (like categories)
    $labels = array(
        'name'              => _x( 'Country', 'taxonomy general name' ),
        'singular_name'     => _x( 'Country', 'taxonomy singular name' ),
        'search_items'      => __( 'Search Country' ),
        'all_items'         => __( 'All Country' ),
        'parent_item'       => __( 'Parent Country' ),
        'parent_item_colon' => __( 'Parent Country:' ),
        'edit_item'         => __( 'Edit Country' ),
        'update_item'       => __( 'Update Country' ),
        'add_new_item'      => __( 'Add New Country' ),
        'new_item_name'     => __( 'New Country Name' ),
        'menu_name'         => __( 'Country' ),
    );

    $args = array(
        'hierarchical'      => true,
        'labels'            => $labels,
        'show_ui'           => true,
        'show_admin_column' => true,
        'query_var'         => true,
        'rewrite'           => array( 'slug' => 'country' ),
    );

    register_taxonomy( 'country', array( 'banner' ), $args );
}

Answer

i think you posted the different code earlier. But if this is the code you are using right now then you should so this

$slide = new wp_query( array ( 'post_type' => 'banner' ,
                                'tax_query' =>
                                          array(
                                            array(
                                              'taxonomy' => 'country', 
                                              'field'    => 'name',
                                              'terms'    => 'algeria'
                                            ),
                                          ),                               
                               'meta_query' => array( 
                                                    'relation' => 'AND',                                                                        
                                                    array('key' => 'page_ads','value' => 'home'),
                                                     array('key' => 'position_ads','value' => 'top')
                                                     )
                            ));

Attribution
Source : Link , Question Author : Zamalek Man , Answer Author : terminator

Leave a Comment