ÁNjanee Shukla
ÁNjanee Shukla

Reputation: 7

Combine and display blog post result combined with custom taxonomy

I created a custom blog page where I am able to list all posts. I have taxonomy post type named news and I want to display post available under this taxonomy mixed with normal posts on same page.

This is my while loop I am using to display post:

while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

    <div class="columns large-12" style="margin-top:10px; border:1px solid #8080804a; padding-left: 0px;" id="2663539596">
    <div class="columns large-6" id="imagesection" style="padding-left: 0px;">
        <p style="background-color: white; padding: 23px; color: black; position: absolute; z-index: 2; font-family: 'Roboto Condensed', Arial, sans-serif; font-weight: bold; margin-top: 75px;"><?php echo get_the_date('jS \of F Y' ); ?> </p>
     <?php the_post_thumbnail('large'); ?>

    </div>

    <div class="columns large-6" style="padding-left:20px; padding-right:20px;">
        <p id="postcat" style="color:white; margin-top:100px; font-family: 'Roboto Condensed', Arial, sans-serif;"> <?php $id = $post->ID; echo esc_html(get_the_category( $id )[0]->name); ?> </p>
    <h2 id="posttitle" style="margin-top:70px;"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" style="color: white; font-family: 'Roboto Condensed', Arial, sans-serif; font-weight:bold;"><?php the_title(); ?></a></h2>




" title="Read More" style="color: white; font-family: 'Roboto Condensed', Arial, sans-serif; margin-left:5px; font-weight:bold"> Read More

so is there any method using which I can combine taxonomy result too with this.

Upvotes: 0

Views: 166

Answers (1)

Lincoln Lemos
Lincoln Lemos

Reputation: 428

what you need is a custom query. You can achieve this with WP_Query.

At your case, you need to create a query with two custom post types (POST and NEWS). Here is the sample code:

$custom_taxonomy = 'your_taxonomy_slug'; // UPDATE to your custom taxonomy slug
$cusom_taxonomy_terms = array( 'term_slug' ); // UPDATE to your custom taxonomy terms
$cpt_slug = 'news'; // UPDATE to your custom post type slug
// set the args for the Query
$args = array(
'post_type' => ['post','news'],
'tax_query' => [
    [
        'taxonomy' => $custom_taxonomy,
        'field'    => 'slug',
        'terms'    => $custom_taxonomy_terms,
    ]
 ]
);

// Create the query
$the_query = new WP_Query( $args );

// The Loop
if ( $the_query->have_posts() ) {
  // Loop start
  while ( $the_query->have_posts() ) {
    $the_query->the_post();
    // UPDATE pasting the loop code below
  }
  // End loop
} else {
 // No posts found
}

/* Restore original Post Data */
wp_reset_postdata();

Upvotes: 0

Related Questions