ThemeShaper Forums » Thematic

[closed]

retireve post based on category and or tag

(12 posts)
  • Started 11 years ago by AnotherAndrew
  • Latest reply from AnotherAndrew
  • This topic is resolved
  1. AnotherAndrew
    Member

    I would like to be able to retrieve a post in a category based on if it has a certain tag.

    Or in other words, I would like my loop to return the latest post from a certain category BUT if it has a tag of 'featured' then it would return the latest post with that tag first.

    If the post does not have the tag 'featured' associated, then it would return the next latest post from that category without the tag 'featured'.

    Maybe, ideally, the post could have a custom field for 'featured'?

    I am using this code but not getting the results I want. Anyone have suggestions?

    <?php
    	if (is_category(10) || is_tag('featured')) {
    	$featurednews = new WP_Query( 'tag=featured&cat=10&posts_per_page=1&orderby=date' );
    	};
    	else (is_category(10) {
    	$featurednews = new WP_Query( 'cat=10&posts_per_page=1&orderby=date' );
    	}
    	while ( $featurednews->have_posts() )
    	{
    		$featurednews->the_post(); // fill $post with data
    		?>
    			<div class="post" id="post-<?php the_ID(); ?>">
    				<div class="home-thumbnail">
    					<a href="<?php the_permalink() ?>"><?php the_post_thumbnail(array(160,160))?></a>
    				</div>
    				<div class="text">
    					<h2>latest news: <?php the_date('F j, Y');?>
    </h2>
    					<h3><?php the_title() ?></h3>
    					<?php the_excerpt() ?>
    				</div>
    			</div>
    		<?php
    	}
    	rewind_posts();
    ?>
    Posted 11 years ago #
  2. middlesister
    Member

    Well is_tag() checks whether the page is a tag archive page, so it will never be true on a category page.
    Maybe you could try with has_tag( 'featured' ) instead? I think it uses the current post in the loop, and if called outside the loop needs a post ID parameter has_tag( 'featured', ID).
    I'm not sure I follow the logic of what you want to do. Would you remove the featured tag each time you want a 'normal' category post to be displayed?

    Posted 11 years ago #
  3. AnotherAndrew
    Member

    > Would you remove the featured tag each time you want a 'normal' category post to be displayed?

    No, the loop would place priority on a post with a tag of 'featured'. If no posts in the category have a tag of 'featured' then it would select the latest post.

    Posted 11 years ago #
  4. middlesister
    Member

    Ok I see, the category is a fallback. Well try this

    $featurednews = new WP_Query( 'tag=featured&cat=10&posts_per_page=1&orderby=date' );
     if ( $featurednews->have_posts() ) :
        while ( $featurednews->have_posts()) ) : $featurednews->the_post();
            // post formatting goes here
        endwhile;
    else :
       $fallback_news = new WP_Query( 'cat=10&posts_per_page=1&orderby=date' );
        while ($sfallback_news->have_posts()) : $fallback_news->the_post();
    	// post formatting goes here
        endwhile;
        wp_reset_postdata();
    endif;
    wp_reset_postdata();

    I haven't tried nesting things like this, but it ought to work. Maybe you don't need to use reset_postdata twice but only the second one.

    Posted 11 years ago #
  5. AnotherAndrew
    Member

    O, ok, thanks! That seems to make more sense.

    Hmm, your syntax is missing something....

    Posted 11 years ago #
  6. middlesister
    Member

    Sorry, a typo in the second while loop - its $fallback_news and not $sfallback_news of course.
    Or did I miss something else?

    Posted 11 years ago #
  7. AnotherAndrew
    Member

    Yes, I made some edits, but I'm not getting any posts to return.

    Here is my code

    <?php
    $featurednews = new WP_Query( 'tag=featured&cat=10&posts_per_page=1&orderby=date' );
     if ( $featurednews->have_posts() ) :
        while ( $featurednews->have_posts()) : $featurednews->the_post();
            // post formatting goes here
    		echo '			<div class="post" id="post-<?php the_ID(); ?>">
    				<div class="home-thumbnail">
    					<a href="<?php the_permalink() ?>"><?php the_post_thumbnail(array(160,160))?></a>
    				</div>
    				<div class="text">
    					<h2>latest news: <?php the_date(\'F j, Y\');?></h2>
    					<h3><?php the_title() ?></h3>
    					<?php the_excerpt() ?>
    				</div>
    			</div>
    ';
        endwhile;
    else :
       $fallback_news = new WP_Query( 'cat=10&posts_per_page=1&orderby=date' );
        while ($fallback_news->have_posts()) : $fallback_news->the_post();
    	// post formatting goes here
    		echo '			<div class="post" id="post-<?php the_ID(); ?>">
    				<div class="home-thumbnail">
    					<a href="<?php the_permalink() ?>"><?php the_post_thumbnail(array(160,160))?></a>
    				</div>
    				<div class="text">
    					<h2>latest news: <?php the_date(\'F j, Y\');?></h2>
    					<h3><?php the_title() ?></h3>
    					<?php the_excerpt() ?>
    				</div>
    			</div>
    ';
    
        endwhile;
        wp_reset_postdata();
    endif;
    wp_reset_postdata();
    ?>
    Posted 11 years ago #
  8. middlesister
    Member

    The post formatting don't work if you just put everything in echo like that, jump out of php to write the html like this

    <?php
    $featurednews = new WP_Query( 'tag=featured&cat=10&posts_per_page=1&orderby=date' );
     if ( $featurednews->have_posts() ) :
        while ( $featurednews->have_posts()) : $featurednews->the_post();
            // post formatting goes here
    ?>			<div class="post" id="post-<?php the_ID(); ?>">
    				<div class="home-thumbnail">
    					<a href="<?php the_permalink() ?>"><?php the_post_thumbnail(array(160,160))?></a>
    				</div>
    				<div class="text">
    					<h2>latest news: <?php the_date('F j, Y');?></h2>
    					<h3><?php the_title() ?></h3>
    					<?php the_excerpt() ?>
    				</div>
    			</div>
    <?php
        endwhile;
    else :
       $fallback_news = new WP_Query( 'cat=10&posts_per_page=1&orderby=date' );
        while ($fallback_news->have_posts()) : $fallback_news->the_post();
    	// post formatting goes here
    ?>			<div class="post" id="post-<?php the_ID(); ?>">
    				<div class="home-thumbnail">
    					<a href="<?php the_permalink() ?>"><?php the_post_thumbnail(array(160,160))?></a>
    				</div>
    				<div class="text">
    					<h2>latest news: <?php the_date('F j, Y');?></h2>
    					<h3><?php the_title() ?></h3>
    					<?php the_excerpt() ?>
    				</div>
    			</div>
    <?php
        endwhile;
        wp_reset_postdata();
    endif;
    wp_reset_postdata();
    ?>

    Like I said it ought to work, but I haven't tested it yet. I'll come back when I have.

    Posted 11 years ago #
  9. AnotherAndrew
    Member

    You the man. That does work.

    Just so you know, you do need to get rid of the last wp_reset_postdata() or else, at least in my case, I got a 403 on my admin login page.

    Posted 11 years ago #
  10. middlesister
    Member

    Cool! Ok, good to know for the future.

    Posted 11 years ago #
  11. AnotherAndrew
    Member

    O, wow, I did not know about this sticky post option.

    This is just what I was thinking about and looking for.

    How does that work?

    Posted 11 years ago #
  12. AnotherAndrew
    Member

    I am answering my own question for future time travelers!

    To use the sticky post feature, I parsed in an array into the wp query that would include the sticky post option, like so

    $args=array(
        'post_type' => 'post',
        'post_status' => 'publish',
        'post__in'=>get_option('sticky_posts'),
        'category__in'=> array (10),
        'showposts' => 1,
        'caller_get_posts'=> 1
        );
    
    $featurednews = new WP_Query( $args );
    // ETC from the code above in this discussion
    Posted 11 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.