Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

custom loop to exclude posts belonging to a custom taxonomy WordPress

  • SOLVED

I have two quick loop questions, both directly related.

1. I use woothemes, and they have a custom taxonomy 'tumblog' which has terms 'audio,video,post,image,quote,and link'.
(user can also add in their own custom tumblog cats, those listed are built-in)

This is simply a custom taxonomy for the 'posts' on the site.

What I need:
1. A loop I can run to exclude all posts that belong to the 'tumblog' taxonomy -(if there is any tumblog term assigned to the post, do not display the post in the loop).

2. A loop I can run to exclude all posts with the term 'quote' from the tumblog custom taxonomy.

Reasons:
1. I use a loop in my homepage slider to display posts, and the tumblog posts are totally screwing up my slider because tumblog posts most often do not have images assigned to them.

2. On my archive page, I don't want quotes showing up at all. I have other plans for quotes.

Answers (1)

2010-12-05

Michael Fields answers:

Let me know if this works for number 1
$posts = $wpdb->get_results( "
SELECT p.*
FROM $wpdb->posts AS p, $wpdb->term_taxonomy as tt, $wpdb->term_relationships as tr
WHERE tt.`taxonomy` != 'tumblog'
AND tt.`term_taxonomy_id` = tr.`term_taxonomy_id`
AND tr.`object_id` = p.ID
AND p.`post_type` = 'post'
GROUP BY p.`ID`
" );


shawn comments:

I'm in church at the moment, so don't have time to test right now. Will do this evening.

I've got to figure out how to merge your code in with mine. (I've seen code formatted like that, but have not worked with it yet, more used to standard wp loop code, more than willing to learn though)

For reference, here is the loop I am using for #1


<?php $saved = $wp_query; query_posts(array('tag__in' => $tag_array, 'showposts' => $woo_options['woo_panel_1_post_count'], 'cat' => $feat_id)); ?>
<?php if (have_posts()) : $count = 0; ?>
<?php while (have_posts()) : the_post(); global $post; $shownposts[$count] = $post->ID; $count++; ?>

<!-- do stuff -->

<?php endwhile; $wp_query = $saved; ?>
<?php wp_reset_query(); ?>


Michael Fields comments:

If you can wait for 3.1 you will be able to do this for #2:


$myquery['tax_query'] = array(
array(
'taxonomy' => 'tumblog',
'terms' => array('quote'),
'field' => 'slug',
'operator' => 'NOT IN',
),
);
query_posts($myquery);


Really, with 3.1 my answer for #1 would not be necessary either, you coulfd use something very similar to this, just list all tumblog terms in the "terms" argument.


shawn comments:

#2 is more a standard loop

<code>
<?php
// WP 3.0 PAGED BUG FIX
if ( get_query_var('paged') )
$paged = get_query_var('paged');
elseif ( get_query_var('page') )
$paged = get_query_var('page');
else
$paged = 1;
//$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

query_posts("paged=$paged");
?>
<?php if (have_posts()) : $count = 0; ?>
<?php while (have_posts()) : the_post(); $count++; ?>

<!-- do stuff -->

<?php endwhile; else: ?>
<?php endif; ?>


Michael Fields comments:

Same kind of deal with a bit different syntax.


$posts = get_posts();

$post_backup = $post;
foreach( (array) $posts as $post ) {
setup_postdata( $post );
the_title();
the_content();
}
$post = $post_backup;


shawn comments:

Yeah, I can't wait for 3.1 as I love the new functionality.

The problem I have is this code is for a commercial theme release, and there is no way I can count on the users having 3.1 installed. I've got to support 3.0+ and then use updated code sometime around 3.2


Michael Fields comments:

Are you displaying this stuff on a page? It looks like you have a nested loop in that last example?


shawn comments:

Yes, displaying #2 in a page:

#1 example:
http://demo.theanointedone.com/
(tab #4 on the slider, is currently showing posts, unfortunately showing tumblog posts as they are most recent, hence need for code #1)

#2 example:
http://demo.theanointedone.com/blog/

notice all the tumblogs on the page. I only want to remove the 'quote' tumblog from this loop, so that I can actually put it into the sidebar using some rotating js.

*Server is rather slow at the moment as it's getting hammered due to live broadcasts going on right now. Seriously need to move to a cdn soon.


Michael Fields comments:

As Far as I know, you'll need custom sql for #2 in 3.0. I can probably whip this up if you're cool with using raw sql.... It's generally considered "bad" form especially for the front end.


shawn comments:

I personally have zero experience with raw sql. However it is important enough to me that I don't mind for now. Maybe I can simply use the raw sql in my theme functions.php file and then simply use a variable on the front end page template, so that most people don't see what is happening.

I'm guessing these 2 scripts are probably more time-consuming than I anticipated. Let me know what you want to provide me with the 2 loops etc, and within reason, I'll increase the bid.

(I'd appreciate it if you simply provide scripts that I can cut 'n paste so that I'm not straining to figure everything out tonight. Long day with church going on etc)

*I don't feel so inept anymore LOL, I'd tried every combination of $args that I could think of and nothing was working. Good to know it takes real experts to do this.


shawn comments:

Church service #2 is over, heading out, back in a few hrs or so for #3.

Thnx Michael


Michael Fields comments:

Unfortunately, the query I posted before is next to useless. I was completely thinking about this wrong and am having a hard time figuring out how you would go about doing this in 3.0. I believe that the best answer to your question would be to use 3.1's features and possibly drop support for 3.0 all together. I know this is probably not what you want to hear.

1. Get posts that are not tumblogs:

$taxonomy = 'tumblogs';

$terms = array();
$_terms = get_terms( $taxonomy, array( 'fields' => 'names' ) );
if ( is_array( $_terms ) ) {
$terms = $_terms;
}

global $query_string;
$myquery = wp_parse_args( $query_string );
$myquery['tax_query'] = array(
array(
'taxonomy' => $taxonomy,
'terms' => $terms,
'field' => 'slug',
'operator' => 'NOT IN'
)
);

query_posts( $myquery );


2. Get all posts that are not quotes:
global $query_string;
$myquery = wp_parse_args( $query_string );
$myquery['tax_query'] = array(
array(
'taxonomy' => 'tumblog',
'terms' => array( 'quote' ),
'field' => 'slug',
'operator' => 'NOT IN'
)
);

query_posts( $myquery );


shawn comments:

Do you think there is a way to do #1 in 3.0?

*It completely screws up my slider that displays posts, when I end up with tumblogs like video,audio,quotes, etc showing up.

In the meantime, I may have to figure out a loop for the slider, that says if there is no image in the post, either wp featured-image, custom-field, or in the post, then skip the post from the loop.

I'm guessing the functions you just posted are for 3.1?


Michael Fields comments:

If I were creating this theme, I would create a custom UI where the user could specify the posts that should be featured. I would then save their ID's of the posts chosen in an option which I could then pass to get_posts()'s include parameter.


shawn comments:

You are correct, I have already created an admin UI where the user can choose the category, terms, or combination of both to show in the slider. It only 'messes us' when the user chooses 'all categories'.

Maybe I am just paraniod and to worried about getting everything absolutely perfect.

I appreciate your time on this one, and will choose you as the winner for the advice given today.

thanks again