wp_query taxonomy code example

Example 1: wp_query get by taxonomy

$the_query = new WP_Query( array(
    'post_type' => 'Adverts',
    'tax_query' => array(
        array (
            'taxonomy' => 'advert_tag',
            'field' => 'slug',
            'terms' => 'politics',
        )
    ),
) );

  // The Loop
    if ( $the_query->have_posts() ) {
        while ( $the_query->have_posts() ) {
          $the_query->the_post();
          the_title(); //show post title 
        }
    }

Example 2: wp query

<?php 
// The Query
$the_query = new WP_Query( $args );
 
// The Loop
if ( $the_query->have_posts() ) {
    echo '<ul>';
    while ( $the_query->have_posts() ) {
        $the_query->the_post();
        echo '<li>' . get_the_title() . '</li>';
    }
    echo '</ul>';
} else {
    // no posts found
}
/* Restore original Post Data */
wp_reset_postdata();

Example 3: wp_query order by taxonomy

$timeline_taxonomies = get_terms(array(
    'fields' => 'ids',   //get the IDs
    'taxonomy'     => 'taxonomy_name',
    'orderby'      => 'term_order',
    'hide_empty'   => true,
));

Example 4: wp query search

$query = new WP_Query( array( 's' => 'keyword' ) );

Tags:

Php Example