[Plugin: Simple Taxonomies] Query all posts for custom taxonomy in 2.8 (5 posts)

  1. trex33
    Posted 7 years ago #

    The ability to add new custom taxonomies under 2.8 is fantastic! One question though - How can you use query_posts to show all posts filed under a particular taxonomy that you created (e.g., "People")?

    For example, this will show all posts filed under "Big Bird" in the "People" taxonomy:
    <?php query_posts('people=big-bird'); ?>

    But how do you query all posts filed under "People" (not just those posts relating to Big Bird)???

  2. Justin Tadlock
    WordPress God
    Posted 7 years ago #

    I think you're missing the idea here. For example, let's look at your question from the perspective of tags and categories.

    Your question:

    But how do you query all posts filed under "People" (not just those posts relating to Big Bird)???

    That's the same as asking:

    * How do you query all posts filed under "Categories"?
    * How do you query all posts filed under "Post Tags"?

    Do you understand what I'm getting at? "People" is just a label that defines what the taxonomy is.

    Granted, you could probably write a function like has_people() to check if a post has people in it. Of course, you're not really querying anything there. You might be able to write a custom select query for thigh though.

  3. trex33
    Posted 7 years ago #

    Thanks Greenshady. I think I see what you mean. Lately I've been using category templates instead of posts, which is where I got confused.

    In your People>Will Smith example, I would create a post (instead of term description) to use as my main content block for a Will Smith page. Then I create a People template. In the template, I pull in the description post about Will Smith. I then have the freedom to create separate queries for related content (e.g., Horror Movies tagged with Will Smith, Comedies tagged with Will Smith, etc.).

    So in the end, you get a page about Will Smith that has loads of related content. Anyhow, thanks for clearing that up for me and keep up the great work!

  4. vvvvv
    Posted 7 years ago #

    hey justin,

    i think trex33's request is valid though. i.e if you reference the structure of a portfolio:

    for instance, let's say we create WORK as a new taxonomy. and i can give it terms, such as illustration, branding, logotype, etc. I'm sure it's very practical to want an instance where all terms tax'ed as WORK to be displayed and queryed in a single instance for people to browse. As well, to give the user the certain impression of density and validity in UI that some people feel comfortable with when viewing people's work.

    what do you think? thanks.
    big ups for the taxonomy write-up on your blog.

  5. Michael Fields
    Posted 6 years ago #

    I found this thread while trying to accomplish the same thing. Here's the solution that I came up with for a blog running WordPress 2.9.2 that has the taxonomy Artist Type defined only for Pages. I needed to query all pages that had a term of this taxonomy associated to them. Here's what I did:

    $artist_ids = array();
    $artists = $wpdb->get_results( "
    	SELECT p.*
    	FROM $wpdb->posts AS p, $wpdb->term_taxonomy as tt, $wpdb->term_relationships as tr
    	WHERE tt.taxonomy = 'artist-type'
    	AND tt.term_taxonomy_id = tr.term_taxonomy_id
    	AND tr.object_id = p.ID
    	AND p.post_type = 'page'
    	AND p.post_status = 'publish'
    	ORDER BY p.menu_order ASC
    	" );
    if ( $artists ) {
    	/* Create an array filled with the ids of all artist posts. */
    	foreach( $artists as $artist )
    		$artist_ids[] = $artist->ID;
    	/* Make sure the postmeta is cached. */
    	if( !empty( $artist_ids ) )
    		update_meta_cache( 'post', $artist_ids );
    	/* Loop over artists */
    	foreach( $artist_pages as $post ) {
    		setup_postdata( $post );
    		#Do wordpress loop stuff here.

    I know using custom queries is not really the best practice, but I also don't see the database structure change very much either.

    Hope someone finds this useful.

Topic Closed

This topic has been closed to new replies.

About this Topic