[wp-hackers] wp-hackers Digest, Vol 66, Issue 101

Mike Schinkel mikeschinkel at newclarity.net
Mon Jul 26 20:56:30 UTC 2010


Hi Kris:

I was intrigued to find a solution for "taxonomy=clients" and "term=any" where "any" is a wildcard (WP_Query() supports "any" as a wildcard for post_types but not for terms of a taxonomy.)

Here's a hook that adds an "any" wildcard for terms for a taxonomy assuming some other plugin didn't muck with the " AND 0 " inserted into the WHERE clause when a taxonomy term doesn't match:

add_action('posts_where','my_posts_where',10,2);

function my_posts_where($where,$query) {
	if (isset($query->query_vars['taxonomy']) && $query->query_vars['term']=='any') {
		global $wpdb;
		$tt = $wpdb->term_taxonomy;
		$tr = $wpdb->term_relationships;
		$where .= $wpdb->prepare(" AND {$wpdb->posts}.ID IN (
						SELECT object_id FROM $tr
						INNER JOIN $tt ON $tt.term_taxonomy_id = $tr.term_taxonomy_id
						WHERE $tt.taxonomy=%s) ",$query->query_vars['taxonomy']);
		$where = str_replace(' AND 0 ','',$where);
	}
	return $where;
}

Your query for this would look like this:

$posts = new WP_Query("post_type=portfolio_item&taxonomy=clients&term=any&posts_per_page=-1");

Or like this:

$posts = new WP_Query(array(
	'post_type' => 'portfolio_item',
	'taxonomy' => 'clients',
	'term' => 'any',
	'posts_per_page' => -1,
));

It works but unfortunately it's not very robust because of the need to remove the " AND 0 " from the WHERE clause because there are other reasons get_posts() adds " AND 0 ".  If query natively supported "term=any" or at least if there was a hook in /wp-includes/query.php (lines 2049 and 2068) that allowed us to target this specifically it might be better; if any of the core team agree I'll add a ticket to trac.

Alternate, maybe someone else knows a better way I didn't discover?

-Mike


On Jul 26, 2010, at 10:53 AM, Kris Young wrote:

> I was as nice as I could be in my original mail without getting
> frustrated - it really helps though that someone else is having the same
> problem though. I honestly think this whole process was a lot more
> complicated than it should have been.
> 
> Thanks for your solution - I will do my best to take a look at it today,
> and will let you know when I make some progress. Thanks again, and also
> to Mike Schinkel for the same.
> 
> Kris
> 
> -----Original Message-----
> From: wp-hackers-bounces at lists.automattic.com
> [mailto:wp-hackers-bounces at lists.automattic.com] On Behalf Of Jared
> Williams
> Sent: 26 July 2010 14:23
> To: wp-hackers at lists.automattic.com
> Subject: Re: [wp-hackers] wp-hackers Digest, Vol 66, Issue 101
> 
> I have a wp_nav_menu, and it has two top level pages that have a bunch
> of
> terms I've created for two custom taxonomies, which are for the two top
> level pages. I'm trying to figure out how I can hide the terms from the
> drop
> down menu I made that shows the terms, if there are no posts assigned to
> a
> term of the taxonomies. Is this possible? It doesn't seem like it is.
> 
> 
> As for Kris Young's question about showing all posts of a taxonomy, this
> is
> a problem I ran into as well, and searched for a long time and found it
> very
> hard to figure out.
> 
> What I managed to do is list all terms for a taxonomy, and all the posts
> for
> each term individually. You may be able to hack this to do what you're
> trying to do since it is the same as what I needed to do I just didn't
> have
> time to make it list only the posts for the taxonomy.
> 
> 
> <?php
> // Create list of taxonomy terms and list the posts under each term
> $post_type = 'post';
> $tax = 'your_custom_taxonomy';
> $tax_terms = get_terms( $tax );
> if ($tax_terms) {
>    foreach ($tax_terms  as $tax_term) {
>    $args = array(
>        'post_type' => $post_type,
>        "tax" => $tax_term->slug,
>        'post_status' => 'publish',
>        'posts_per_page' => -1,
>        'caller_get_posts'=> 1
>    );
> 
>        $my_query = null;
>        $my_query = new WP_Query($args);
> 
>        if( $my_query->have_posts() ) : ?>
> 
>            <h2 class="breadcrumb">All <?php echo $tax; ?> Posts For
> <?php
> echo $tax_term->name; ?></h2>
>            <ul class="taxlist">
>            <?php while ( $my_query->have_posts() ) :
> $my_query->the_post();
> ?>
> 
>                <li id="post-<?php the_ID(); ?>">
>                    <a href="<?php the_permalink(); ?>" rel="bookmark"
> title="Permanent Link to <?php the_title_attribute(); ?>"><?php
> the_title();
> ?></a>
>                </li>
> 
> 
>            <?php endwhile; // end of loop ?>
>            </ul>
> 
>            <?php if( function_exists('wp_pagenavi')) { wp_pagenavi(); }
> else { ?>
>                <div class="navigation clearfix">
>                    <div class="alignleft"><?php
> next_posts_link('&laquo;
> Previous Entries') ?></div>
>                    <div class="alignright"><?php
> previous_posts_link('Next
> Entries &raquo;') ?></div>
>                </div>
>            <?php } ?>
> 
>        <?php else : ?>
> 
>            <h2 class="title">Oops</h2>
>            <p>Looks like something is missing...</p>
> 
>        <?php endif; // if have_posts()
>        wp_reset_query();
> 
>    } // end foreach #tax_terms
> }
> ?>
> 
> Hope this helps.
> 
> Jared Williams
> 
> 
> On Mon, Jul 26, 2010 at 8:00 AM,
> <wp-hackers-request at lists.automattic.com>wrote:
> 
>> Send wp-hackers mailing list submissions to
>>       wp-hackers at lists.automattic.com
>> 
>> To subscribe or unsubscribe via the World Wide Web, visit
>>       http://lists.automattic.com/mailman/listinfo/wp-hackers
>> or, via email, send a message with subject or body 'help' to
>>       wp-hackers-request at lists.automattic.com
>> 
>> You can reach the person managing the list at
>>       wp-hackers-owner at lists.automattic.com
>> 
>> When replying, please edit your Subject line so it is more specific
>> than "Re: Contents of wp-hackers digest..."
>> 
>> 
>> Today's Topics:
>> 
>>  1. Re: Custom post types - a multitude of problems (Mike Schinkel)
>>  2. Querying posts of another WordPress install (Ashish Saini)
>>  3. Re: Querying posts of another WordPress install (Ashish Saini)
>>  4. Re: Querying posts of another WordPress install (Aaron Jorbin)
>> 
>> 
>> ----------------------------------------------------------------------
>> 
>> Message: 1
>> Date: Mon, 26 Jul 2010 06:06:14 -0400
>> From: Mike Schinkel <mikeschinkel at newclarity.net>
>> Subject: Re: [wp-hackers] Custom post types - a multitude of problems
>> To: wp-hackers at lists.automattic.com
>> Message-ID: <F70451A1-2EFF-4911-BD5C-ACC86CF452E7 at newclarity.net>
>> Content-Type: text/plain;       charset=us-ascii
>> 
>> On Jul 26, 2010, at 5:20 AM, Kris Young wrote:
>>> I need to output the contents (all posts, all content of those
> posts) of
>>> a specific custom category (taxonomy) and I have no idea how to get
> it
>>> done. All I want is a page that outputs all the posts for a certain
>>> category (and that category only) that I created using this code:
>>> 
>>> register_taxonomy("clients", array("portfolio_item"),
>>> array("hierarchical" => true, "label" => "Clients", "singular_label"
> =>
>>> "Client", "rewrite" => array('slug'=>'portfolio_item')));
>>> 
>>> Yes, that part was simple, but for some reason my custom taxonomy is
>>> completely separate from the rest of the site and irretrievable.
> Can't
>>> figure out for the life of me how to do this.
>> 
>> To start, I think you have your taxonomy registration wrong. (FWIW my
>> preference would be to go with a singular for the taxonomy name, e.g.
>> "client" but I'll leave yours as is):
>> 
>> $labels = array(
>>   'name' => _x( 'Clients', 'taxonomy general name' ),
>>   'singular_name' => _x( 'Client', 'taxonomy singular name' ),
>>   'search_items' =>  __( 'Search Clients' ),
>>   'all_items' => __( 'All Clients' ),
>>   'parent_item' => __( 'Parent Client' ),
>>   'parent_item_colon' => __( 'Parent Client:' ),
>>   'edit_item' => __( 'Edit Client' ),
>>   'update_item' => __( 'Update Client' ),
>>   'add_new_item' => __( 'Add New Client' ),
>>   'new_item_name' => __( 'New Client Name' ),
>> );
>> 
>> register_taxonomy("clients", array("portfolio_item"),array(
>>       "hierarchical" => true,
>>       "labels" => $labels,
>>       "public" => true,
>>       "show_ui" => true,
>>       "query_var" => "clients",
>>       "rewrite" => array('slug'=>'client'),
>> ));
>> 
>> Many of these options will default but I went ahead and listed them
> all.
>> Read more here:
>> 
>> http://codex.wordpress.org/Function_Reference/register_taxonomy
>> 
>> As for the rest of your question, I'm unclear.  Do you want all
> Portfolio
>> Items that have *any* clients, or a specific client?  For a specific
> client
>> i.e. "john-smith" I think this would be what you need (though I
> haven't run
>> the code so it may have typos):
>> 
>> <?php
>> $portfolio_items = new
>> 
> WP_Query('post_type=portfolio_item&taxonomy=clients&term=john-smith&post
> s_per_page=-1');
>> if ( $portfolio_items->have_posts() ):
>>       while ( $portfolio_items->have_posts() ) :
>> $portfolio_items->the_post(); ?>
>>               <h2 id="post-<?php the_ID(); ?>" <?php post_class();
>> ?>><?php the_title(); ?></h2>
>>               <?php the_content();
>>       endwhile;
>> endif;
>> 
>> If you want to get all portfolio items that have *any* clients I've
>> actually not done that before so I'll have to let someone else answer.
>> 
>> HTH.
>> 
>> -Mike
>> 
>> ------------------------------
>> 
>> Message: 2
>> Date: Mon, 26 Jul 2010 17:08:30 +0530
>> From: Ashish Saini <ashishsainiashfame at gmail.com>
>> Subject: [wp-hackers] Querying posts of another WordPress install
>> To: wp-hackers <wp-hackers at lists.automattic.com>
>> Message-ID:
>>       <AANLkTi=CHq9bUukSa19MgJVSUU1CuQYfzUj6RBvmy3NS at mail.gmail.com>
>> Content-Type: text/plain; charset=ISO-8859-1
>> 
>> Hi,
>> 
>> Is there any way that I can query posts from another WordPress install
>> residing in the same database? I know it can be done via SQL queries
> but is
>> there any workaround hacking my way along with API?
>> 
>> Thanks & Regards
>> Ashish Saini aka Ashfame
>> (Deserve Before You Desire)
>> Homepage : http://www.ashfame.com | Blog : http://blog.ashfame.com |
>> Twitter
>> : http://twitter.com/ashfame
>> 
>> 
>> ------------------------------
>> 
>> Message: 3
>> Date: Mon, 26 Jul 2010 17:12:22 +0530
>> From: Ashish Saini <ashishsainiashfame at gmail.com>
>> Subject: Re: [wp-hackers] Querying posts of another WordPress install
>> To: wp-hackers <wp-hackers at lists.automattic.com>
>> Message-ID:
>>       <AANLkTimmte-eFUkmk2w3KsaZpWkHCKtD=pxU4OnPj0XA at mail.gmail.com>
>> Content-Type: text/plain; charset=ISO-8859-1
>> 
>> I think this can be done with the help of setup_postdata() by
> populating
>> the
>> fields as explained here -
>> 
> http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query
>> 
>> Right? And is this the best approach?
>> 
>> Thanks & Regards
>> Ashish Saini aka Ashfame
>> (Deserve Before You Desire)
>> Homepage : http://www.ashfame.com | Blog : http://blog.ashfame.com |
>> Twitter
>> : http://twitter.com/ashfame
>> 
>> 
>> On Mon, Jul 26, 2010 at 5:08 PM, Ashish Saini
>> <ashishsainiashfame at gmail.com>wrote:
>> 
>>> Hi,
>>> 
>>> Is there any way that I can query posts from another WordPress
> install
>>> residing in the same database? I know it can be done via SQL queries
> but
>> is
>>> there any workaround hacking my way along with API?
>>> 
>>> Thanks & Regards
>>> Ashish Saini aka Ashfame
>>> (Deserve Before You Desire)
>>> Homepage : http://www.ashfame.com | Blog : http://blog.ashfame.com |
>>> Twitter : http://twitter.com/ashfame
>>> 
>> 
>> 
>> ------------------------------
>> 
>> Message: 4
>> Date: Mon, 26 Jul 2010 07:56:17 -0400
>> From: Aaron Jorbin <aaron at jorb.in>
>> Subject: Re: [wp-hackers] Querying posts of another WordPress install
>> To: wp-hackers at lists.automattic.com
>> Message-ID:
>>       <AANLkTin6x+4NT6E6bxR=1Xq43D6txPhT0et2xnK_45Nt at mail.gmail.com>
>> Content-Type: text/plain; charset=ISO-8859-1
>> 
>> Is this using Multisite?  If so, you can use switch_to_blog -
>> http://codex.wordpress.org/WPMU_Functions/switch_to_blog and it's
>> counterpart restore_current_blog -
>> http://codex.wordpress.org/WPMU_Functions/restore_current_blog to move
>> around use the api.
>> 
>> 
>> 
>> http://aaron.jorb.in
>> twitter: twitter.com/aaronjorbin
>> 
>> 
>> On Mon, Jul 26, 2010 at 7:42 AM, Ashish Saini
>> <ashishsainiashfame at gmail.com>wrote:
>> 
>>> I think this can be done with the help of setup_postdata() by
> populating
>>> the
>>> fields as explained here -
>>> 
> http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query
>>> 
>>> Right? And is this the best approach?
>>> 
>>> Thanks & Regards
>>> Ashish Saini aka Ashfame
>>> (Deserve Before You Desire)
>>> Homepage : http://www.ashfame.com | Blog : http://blog.ashfame.com |
>>> Twitter
>>> : http://twitter.com/ashfame
>>> 
>>> 
>>> On Mon, Jul 26, 2010 at 5:08 PM, Ashish Saini
>>> <ashishsainiashfame at gmail.com>wrote:
>>> 
>>>> Hi,
>>>> 
>>>> Is there any way that I can query posts from another WordPress
> install
>>>> residing in the same database? I know it can be done via SQL
> queries
>> but
>>> is
>>>> there any workaround hacking my way along with API?
>>>> 
>>>> Thanks & Regards
>>>> Ashish Saini aka Ashfame
>>>> (Deserve Before You Desire)
>>>> Homepage : http://www.ashfame.com | Blog : http://blog.ashfame.com
> |
>>>> Twitter : http://twitter.com/ashfame
>>>> 
>>> _______________________________________________
>>> wp-hackers mailing list
>>> wp-hackers at lists.automattic.com
>>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>>> 
>> 
>> 
>> ------------------------------
>> 
>> _______________________________________________
>> wp-hackers mailing list
>> wp-hackers at lists.automattic.com
>> http://lists.automattic.com/mailman/listinfo/wp-hackers
>> 
>> 
>> End of wp-hackers Digest, Vol 66, Issue 101
>> *******************************************
>> 
> 
> 
> 
> -- 
> Thank you,
> 
> Jared Williams
> Web/Graphic Designer
> Tweeaks.com  |  New2WP.com
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers



More information about the wp-hackers mailing list