PhilGarrett
Member
Posted 2 years ago #
I am currently putting together a site that lists a bunch of items in the archive page. Some of the items have been tagged with with a keyword that then puts a little image next to the post title to show its different from the rest.
Currently, the posts are listed in order by date, I was wondering if there was anyway to modify the loop to have it pull the posts with that tag word first and then the remaining posts after that. Or would I have to look at running the loop twice?
Any information would be much appreciated.
Thanks.
Put this before your other loop:
<?php
//get tag ID 41, and display posts in each tag
$taxonomy = 'post_tag';// e.g. post_tag, category
$param_type = 'tag__in'; // e.g. tag__in, category__in
$term_args=array(
'include' => '41',
'orderby' => 'name',
'order' => 'ASC'
);
$terms = get_terms($taxonomy,$term_args);
if ($terms) {
foreach( $terms as $term ) {
$args=array(
"$param_type" => array($term->term_id),
'post_type' => 'post',
'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() ) {
echo 'List of Posts in '.$taxonomy .' '.$term->name;
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
<?php
endwhile;
}
}
}
wp_reset_query(); // Restore global post data stomped by the_post().
?>