Skip to content

Instantly share code, notes, and snippets.

@ericpedia
Created August 30, 2011 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericpedia/1182069 to your computer and use it in GitHub Desktop.
Save ericpedia/1182069 to your computer and use it in GitHub Desktop.
<?php /***
Man after thinking about this for some time I can't think of a good way to do this without doing a lot of huge queries. Hmmm…
Well you have to have a query to get all of the posts so you can determine how many pages of results you're dealing with. You might be better off writing a custom SQL query for this and just selecting the ID column. It will be quicker than selecting a whole bunch of other columns you don't really need.
Once you have all of the post IDs, you can figure out which terms are relevant for each taxonomy using wp_get_object_terms (pass it an array of IDs and a string for the taxonomy).
Loop over this array of term objects and make a new array that just contains term IDs. Now you can loop over all of the terms in the taxonomy and do a conditional to determine if the term ID is in the array of matched IDs we made earlier. If it is, display it as bold in your drop down, otherwise gray it out.
You're going to have to do a lot of experimenting to get it working just right but that should help get you started. Here's some pseudo code:
***/
//A bunch of logic to determine what options we're dealing with and to set up default options.
$option = 'default'
if( isset($_GET['option']) ) {
$option = $_GET['option']
//You might also want to do some validation to make sure the data is what you think and prevent attacks. See the kses functions in http://codex.wordpress.org/Function_Reference/#Formatting_Functions
}
//Custom SQL query to get only the IDs of the posts that match the parameters.
$matching_post_ids = [1,2,3,4,6...];
$taxonomies = array('tax1', 'tax2'); //It's more effienct to list them as a static array rather than querying the database.
foreach($taxonomies as $tax) {
//We need to find all the terms for the matched posts
$args = array('orderby' => 'name', 'order' => 'ASC', 'fields' => 'ids');
$yup_terms = wp_get_object_terms($matching_post_ids, $tax, $args); //See http://codex.wordpress.org/Function_Reference/wp_get_object_terms
$args = array(
'orderby' => 'name',
'hide_empty' => 1 //Don't show terms that aren't assigned to anything. Kind of silly to show a term that will always return 0 results no matter what
);
$all_terms = get_terms( array($tax), $args );
foreach( $all_terms as $term ) {
if( in_array($term->term_id, $yup_terms )) {
//This term has matches.
} else {
//This term has no matches.
}
}
}
//Real $Wp_Query to get all the details of the posts we want to display on one page. I prefer the get_posts function which can take the same parameters as Wp_Query. See http://codex.wordpress.org/Template_Tags/get_posts
$args = array(
'numberposts' => 10,
'offset' => $what_page_are_we_on,
'post_type' => array('your_post_type1', 'your_post_type_2'),
... blah blah blah ...
);
foreach( get_posts($args) as $post ):
setup_postdata($post);
?>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<?php
endforeach;
// Just experiment and have fun with it. Eventually you'll get something that works.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment