Ask your WordPress questions! Pay money and get answers fast! (more info)

Query to return posts in the same category but different PT WordPress

  • SOLVED

We are creating a website with some custom post types and taxonomies which will store different types of data, each of them with a custom taxonomy, something like:
- "Jobs" CPT with "Jobs Categories"
- "Events" CPT with "Events Categories"
- "Downloads" CPT with "Downloads Categories"

We also have a CPT designed to store page header images, called "Headers". All the custom taxonomies are enabled for the "Headers" type, so a "Header" can be a member of any or of multiple "Job Categories", "Events Categories" and/or "Downloads Categories".

What we need is a way to display the respective header image on category pages of the "Job Categories", "Events Categories" and "Downloads Categories" within the post types. It is basically a query which:
1. Checks which category page is currently being displayed
2. Searches for posts with in the same term (same category) but in the "Headers" CPT
3. Displays a random post from the results in 2 (we want to have a random image from a pool showing every time the page is reloaded).

Here is an example in case the description is not clear enough. We have the following assets:
- A category "Easy Jobs" within the "Job Categories" taxonomy with some posts
- Since the "Headers" CPT also supports the "Jobs Categories" taxonomy, we also have a "Easy Jobs" category for headers. We have 2 posts in this category - "Header 1" and "Header 2"
- The function we are looking for will be placed before the loop in the "Jobs Categories" category page and will randomly display either "Header 1" or "Header 2"

Answers (2)

2012-08-30

Arnav Joy answers:

can you please check this code for me , if it is displaying all the terms in current taxonomy..


<?php

$term_slug = get_query_var( 'term' );
$taxonomyName = get_query_var( 'taxonomy' );
$current_term = get_term_by( 'slug', $term_slug, $taxonomyName );
$termchildren = get_term_children( $current_term->term_id, $taxonomyName );
echo '<ul>';
foreach ($termchildren as $child) {
$term = get_term_by( 'id', $child, $taxonomyName );
echo '<li><a href="' . get_term_link( $term->name, $taxonomyName ) . '">' . $term->name . '</a></li>';
}
echo '</ul>';

?>



please tell me what you get from this


Arnav Joy comments:

try this


<?php



$term_slug = get_query_var( 'term' );

$taxonomyName = get_query_var( 'taxonomy' );

$current_term = get_term_by( 'slug', $term_slug, $taxonomyName );

$termchildren = get_term_children( $current_term->term_id, $taxonomyName );

echo '<ul>';

foreach ($termchildren as $child) {

$term = get_term_by( 'id', $child, $taxonomyName );

$all_term_id[] = $term->term_id;

echo '<li><a href="' . get_term_link( $term->name, $taxonomyName ) . '">' . $term->name . '</a></li>';

}

echo '</ul>';



?>



<?php


$args = array(

'post_type' => 'Headers',

'order' => 'rand',

'posts_per_page' => 1,

'tax_query' => array(

array(

'taxonomy' => $taxonomyName ,

'field' => 'id',

'terms' => $all_term_id

)

)

);?>



<?php $my_query = new WP_Query(); $my_query->query($args); ?>



<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>

<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></h3>

<?php endwhile; ?>

<?php wp_reset_postdata(); ?>


Yavor Trampov comments:

Hi,

this is not returning anything (just an empty UL). Was I ment to replace something in your script with sote-specific data (like the name of a taxonomy or of a CPT?).


Yavor Trampov comments:

Oh, wait, I did not see the longer answer - will try and write back.


Yavor Trampov comments:

This is returning the following (and no posts):

Notice: Undefined variable: all_term_id in /Volumes/ahorn-design work/Plaut/0001PL_WebsitePlaut/WebDev/wp-content/themes/plaut/archive.php on line 39

Notice: Undefined offset: 0 in /Volumes/ahorn-design work/Plaut/0001PL_WebsitePlaut/WebDev/wp-includes/query.php on line 2234


Arnav Joy comments:

have you created a custom template or any taxonomy file to output this ?


Yavor Trampov comments:

I am using Types + Views (http://wp-types.com) to handle the post-type specific taxonomy pages, so I only have one very basic archive.php file in my theme which should be used for all taxonomies.

2012-08-30

Hai Bui answers:

Please try this: (please check the post type slug if it is "headers" or "header")
<?php
$term_slug = get_query_var( 'term' );
$taxonomyName = get_query_var( 'taxonomy' );

$args = array(
'post_type' => 'headers',
'order' => 'rand',
'posts_per_page' => 1,
$taxonomyName => $term_slug
);

$my_query = new WP_Query();

$my_query->query($args);

while ($my_query->have_posts()) : $my_query->the_post();
the_content();
endwhile;

wp_reset_postdata();
?>


Yavor Trampov comments:

Hi Hai Bui! Thank you, this seems to work out-of-the-box. I will do some more extensive testing and write back later today.