Forums

[resolved] a complex query posts situation: posts in a category & custom post type taxonomy (10 posts)

  1. tray
    Member
    Posted 6 months ago #

    wow that title is long. lets see if can explain this accurately without writing a novel. :)

    lets start with the desired outcome:
    i have a featured slideshow that i want featured posts AND featured events to appear in.

    • the featured posts are in a category called "Homepage Slideshow" (slug: homepage-slideshow)
    • the featured events are a custom post type in a category (custom taxonomy, "tribe_events_cat") called "Featured Events" (slug: featured-events)

    so i need a query that will grab "posts" in category "homepage-slideshow" AND custom post type "tribe_events" in custom taxonomy "tribe_events_cat" value "featured-events"

    clear as mud? any ideas?

  2. Lisa Sabin-Wilson
    Member
    Posted 6 months ago #

    This should at least get you started, I think. Forgive my 'termies' variable in there - it was late and I was punchy :p

    $tax = $taxonomy = strip_tags( get_the_term_list($post->ID, 'POST_TYPE') );
    		$termies = get_the_terms( $post->id, 'POST_TYPE' );
    		$terms_slugs = array();
    		foreach( $termies as $term ) {
    		$terms_slugs[] = $term->slug; // save the slugs in an array
    		}
    		$query = array(
    			'post_type'=> 'POST_TYPE',
    			'taxonomy' => 'CUSTOM_TAX_NAME',
    			'term' => $term->slug,
    			'category_name' => 'CATEGORY_NAME'
    		);
    	query_posts( $query );

    Or at least close for your purposes? It grabs the term from the postID, gets the term slug then queries: POST_TYPE, TAX, CATEGORY_NAME - yea?

  3. Lisa Sabin-Wilson
    Member
    Posted 6 months ago #

    Actually think I don't need that initial $tax - I think you can ignore that .. its an orphan c/p. :)

  4. Lisa Sabin-Wilson
    Member
    Posted 6 months ago #

    read the initial post, danggit - my query above doesn't really do it.

    Would something like this do it for you?

    $query =
    	array(
    		'post_type'=> 'post',
    		'category_name' => 'Homepage Slideshow',
    	);
    	array (
    		'post_type' => 'tribe_events',
    		'taxonomy' => 'tribe_events_cat',
    		'category_name' => 'Featured Events'
    	);
    query_posts($query);

    not sure?

  5. tray
    Member
    Posted 6 months ago #

    well.. i think we're getting close. i had to tweak the featured events query a bit. (i was over thinking it... like i usually do) now both pieces of this query work. but only the first array shows up in the query.

    here's the code i'm working with:

    $query =
    
    	array(
    		'post_type'=> 'post',
    		'category_name' => 'Homepage Slideshow',
    	);
    	array (
    		'post_type' => 'tribe_events',
    		'eventDisplay' => 'upcoming',
    		'tribe_events_cat' => 'featured-event',
    	);
    
    query_posts($query);

    so now i'm wondering.. is this a limitation of query posts? maybe this needs to be done with two separate queries? ooh. i just made my brain hurt more...

  6. Lisa Sabin-Wilson
    Member
    Posted 6 months ago #

    So the query pulls:
    Posts in Homepage Slideshow
    AND
    CPT = tribe_events in
    Taxonomy - tribe_events_cat with
    term = featured-event
    ?

    $query =
    
    	array(
    		'post_type'=> 'post',
    		'category_name' => 'Homepage Slideshow',
    	);
    	array (
    		'post_type' => 'tribe_events',
    		'taxonomy' => 'tribe_events_cat',
    		'term' => 'featured-event',
    	);
    
    query_posts($query);

    ?

  7. tray
    Member
    Posted 6 months ago #

    no. it's pulling one or the other. whatever array (post type, cat/tax) is declared first. the latter is ignored completely.

    and while i was able to do something like this

    $query =
    	array (
    		'post_type' => array(
    			'post',
    			'tribe_events'),
    );
    query_posts($query);

    which pulled ALL "post" and "tribe_events" entries... as soon as i try to filter out the post category and custom post taxonomy, of course, no entries are returned. (since there is no post that meets the custom tax AND post category)

  8. tray
    Member
    Posted 6 months ago #

    alright. i figured it out with some PHP goodness (and by "i" i mean "the smart PHP minds in the office") so here's what we ended up doing for future searchers of this issue.

    we created TWO queries. one for the custom post type, one for the posts, then merged the two arrays, then set up a "foreach" loop in php. here's the simplified code:

    $query1 =
    	array(
    		'post_type'=> 'post',
    		'category_name' => 'Homepage Slideshow',
    		'showposts' => 5
    	);
    $query2 =
    	array (
    		'post_type' => 'tribe_events',
    		'eventDisplay' => 'upcoming',
    		'tribe_events_cat' => 'featured-event',
    		'showposts' => 5
    	);
    
    $postGroup1 = get_posts( $query1);
    $postGroup2= get_posts( $query2);
    
    $mergedcrap = array_merge($postGroup1, $postGroup2);
    
    foreach ( $mergedcrap as $post )
    {
      echo the_title();
      the_excerpt();
    }
  9. Lisa Sabin-Wilson
    Member
    Posted 6 months ago #

    All hail merged crap!

  10. tray
    Member
    Posted 6 months ago #

    i think "$mergedcrap" should be my band name... :)

Reply

You must log in to post.

About this Topic