Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Multiple Loops on Custom Taxonomy Archive View, Different Filters WordPress

  • SOLVED

Hi,

Here’s what I’ve done:

- Created a custom post type called “Cars”
- Added a custom field to the custom post type cars called: “carsorder”
- Created a custom post type called “Brand-Overview”
- Created a custom taxonomy called “Brands”
- Assigned the custom taxonomy “Brands” to the post types: “Posts”, “Cars” and “Brand-Overview”

Here’s what I’d like to do:

Modify the “Archive View” of the custom taxonomy “Brands” so, instead of showing all 3 post types (“Posts”, “Cars” and “Brand-Overview”) mixed together in the same list, it shows 3 separate blocks of content on the same page:

1. Just the post type “Brand Overview”. Limited to 1 entry.*
2. Just the post type “Cars”. Limited to 20 entries. Sorted by the custom field “carsorder”.*
3. Just the blog “Posts”. Limited to 3 entries. *

*In all 3 cases the entries need to be filtered by the taxonomy shown in the URL. Eg if the custom taxonomy “Cars” is “BMW”, the URL is:
mydomain.com/cars/bmw
then all 3 separate blocks of content need to be filtered by this term.

The basic code I have in the page template for the archive view is this:

<?php if ( have_posts() ) : ?>

<?php
while ( have_posts() ) : the_post();
get_template_part( 'content', get_post_format() );
endwhile;
?>

<?php else : ?>

<?php endif; ?>


I guess I’ll need multiple loops but I’m not sure how to run The Loop 3 times and filter by a different post type each time.

Thanks for your help!

Answers (1)

2013-06-26

Arnav Joy answers:

try this

<?php if ( have_posts() ) : ?>



<?php

echo ' Post Type:Brand Overview';

$args = array(
'post_type' => 'Brand-Overview',
'posts_per_page' => -1 ,
'tax_query' => array(
array(
'taxonomy' => 'Brands'
)
)
);
$query = new WP_Query( $args );

while ( $query->have_posts() ) : $query->the_post();

get_template_part( 'content', get_post_format() );

endwhile;

echo '<br>';
echo ' Post Type:Cars';

$args = array(
'post_type' => 'Cars',
'posts_per_page' => 20 ,
'tax_query' => array(
array(
'taxonomy' => 'Brands'
)
)
);
$query = new WP_Query( $args );

while ( $query->have_posts() ) : $query->the_post();

get_template_part( 'content', get_post_format() );

endwhile;


echo '<br>';
echo ' Post Type:Posr';

$args = array(
'post_type' => 'post',
'posts_per_page' => 3 ,
'tax_query' => array(
array(
'taxonomy' => 'Brands'
)
)
);
$query = new WP_Query( $args );

while ( $query->have_posts() ) : $query->the_post();

get_template_part( 'content', get_post_format() );

endwhile;

?>



<?php else : ?>



<?php endif; ?>


check proper name of the custom post type in "post_type" field of the array


chrisfdc comments:

Hi Arnav,

Thanks for the fast response although your suggestion doesn’t output anything except the echo stuff.

It seems that the ‘tax_query’ parameter is the problem: I tried your format with just one loop to isolate the issue:

This (your orig) returns a blank screen:

<?php if ( have_posts() ) : ?>

<?php
echo ' Post Type: Brand Overview';
$args = array(
'post_type' => 'brand-overview',
'posts_per_page' => 1 ,
'tax_query' => array(
array(
'taxonomy' => 'brands'
)
)
);
$query = new WP_Query( $args );
while ( $query->have_posts() ) : $query->the_post();
get_template_part( 'content', get_post_format() );
endwhile;
?>


This (taking out the 'tax_query' part) returns the page with the listing but it’s not filtered by the taxonomy:

<?php
echo ' Post Type: Brand Overview';
$args = array(
'post_type' => 'brand-overview',
'posts_per_page' => 1 ,
);
$query = new WP_Query( $args );
while ( $query->have_posts() ) : $query->the_post();
get_template_part( 'content', get_post_format() );
endwhile;
?>



So I guess there’s something not quite right about this part:
'tax_query' => array(
array(
'taxonomy' => 'brands'
)
)


I assume I need to enter the custom taxonomy’s “slug” as the parameter value ie: brands
Although I tried with its slug and its name, both singular and plural: None worked.

It looks very close to being solved so it would be great if you could have a look at that.

Thanks!


Arnav Joy comments:

try this now

<?php

$tax_name = 'brands';
$tax_terms = get_terms( $tax_name , array( 'hide_empty' => 0 ) );
if( !empty( $tax_terms ) ){
foreach( $tax_terms as $term )
$term_id[] = $term->term_id;
}

echo ' Post Type:Brand Overview';


$args = array(
'post_type' => 'brand-overview',
'posts_per_page' => 1 ,
'caller_get_posts'=> 1,
'tax_query' => array(
array(
'taxonomy' => $tax_name,
'field' => 'id',
'terms' => implode(',',$term_id)
)
)
);


$query = new WP_Query( $args );

if ( $query->have_posts() ) :


while ( $query->have_posts() ) : $query->the_post();

get_template_part( 'content', get_post_format() );

endwhile;

?>



<?php else : ?>



<?php endif; ?>

<?php wp_reset_query();?>

<?php
echo '<br>';

echo ' Post Type:Cars';

$args = array(
'post_type' => 'cars',
'posts_per_page' => 20 ,
'caller_get_posts'=> 1,
'tax_query' => array(
array(
'taxonomy' => $tax_name ,
'field' => 'id',
'terms' => implode(',',$term_id)
)
)
);

$query = new WP_Query( $args );

if ( $query->have_posts() ) :


while ( $query->have_posts() ) : $query->the_post();

get_template_part( 'content', get_post_format() );

endwhile;

?>

<?php else : ?>



<?php endif; ?>

<?php wp_reset_query();?>


<?php
echo '<br>';

echo ' Post Type:Post';


$args = array(
'post_type' => 'post',
'posts_per_page' => 3 ,
'caller_get_posts'=> 1,
'tax_query' => array(
array(
'taxonomy' => $tax_name ,
'field' => 'id',
'terms' => implode(',',$term_id)
)
)
);

$query = new WP_Query( $args );

if ( $query->have_posts() ) :


while ( $query->have_posts() ) : $query->the_post();

get_template_part( 'content', get_post_format() );

endwhile;

?>

<?php else : ?>



<?php endif; ?>


Arnav Joy comments:

can you let me know what are you storing in custom field "carsorder"


Arnav Joy comments:

if you are storing any number in it then try replacing this cars args variable with the following :-

$args = array(
'post_type' => 'cars',
'posts_per_page' => 20 ,
'caller_get_posts'=> 1,
'meta_key' => 'carsorder',
'orderby' => 'meta_value_num',
'tax_query' => array(
array(
'taxonomy' => $tax_name ,
'field' => 'id',
'terms' => implode(',',$term_id)
)
)
);


so here is the full code


http://pastie.org/8082887
or second version

http://pastie.org/8082892





chrisfdc comments:

Hi Arnav,

Thanks for the update. I tried both versions of the pastie code in full.
Unfortunately it still gets hung up on the 'tax_query' array. I’ve noticed that using this for the $args works:

$args = array(
'post_type' => 'cars',
'posts_per_page' => 5 ,
'caller_get_posts'=> 1,
'brands' => 'bmw',
);

Rather than:
$args = array(
'post_type' => 'cars',
'posts_per_page' => 20 ,
'caller_get_posts'=> 1,
'tax_query' => array(
array(
'taxonomy' => $tax_name ,
'field' => 'id',
'terms' => implode(',',$term_id)
)
)
);

That is: Referencing the taxonomy without using the 'tax_query' array. Unfortunately this version uses a fixed term: in the example it’s “bmw”. I tried using your “$tax_terms =” code at the beginning and then:

$args = array(
'post_type' => 'cars',
'posts_per_page' => 5 ,
'caller_get_posts'=> 1,
'brands' => $tax_terms,
);


but I think that’s pulling in ALL the terms. My goal is to get the taxonomy term from the URL.
So on the page: mydomain.com/cars/bmw
It shows all the post types that have the taxonomy term “bmw”
and
on the page: mydomain.com/cars/mercedes
It shows all the post types that have the taxonomy term “mercedes”
Etc.
Is there a way to swap the fixed ‘bmw’ for the variable taxonomy term (determined by the URL)?

Thanks!


Arnav Joy comments:

to get specific term's post try this

global $wp_query;
$term_id = $wp_query->get_queried_object_id();

$args = array(

'post_type' => 'cars',

'posts_per_page' => 5 ,

'caller_get_posts'=> 1,

'tax_query' => array(

array(

'taxonomy' => 'brands' ,

'field' => 'id',

'terms' => $term_id

)

)

);


chrisfdc comments:

That works, thanks Arnav. I'll vote all the $20 to you. Have a great rest of the day!