WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] passing current single_tag_title as variable into new wp_query (14 posts)

  1. patcheta
    Member
    Posted 5 years ago #

    Hi there, I'm trying to get the current tag of my post to then pass as a variable into a new wp_query so that I can show other posts that share the same tag. I'm also excluding the current post from the query so that it doesn't show up twice. I just read through all the pages in wp_query support forum and I'm still not getting it. I know it has to be a simple answer. A little new to php so I'm guessing it's my syntax. I'd really appreciate any help. Thanks.

    HERE IS MY CODE:

    <?php $current_tag = single_tag_title(); ?>
    <?php
    $temp = $wp_query;
    $wp_query = null;
    $wp_query = new WP_Query();
    $wp_query->query('tag='. $current_tag);
    ?>
    <?php while ($wp_query->have_posts()) : $wp_query->the_post();
    if( $post->ID == $do_not_duplicate ) continue;
    update_post_caches($posts); ?>

  2. Justin Tadlock
    Member
    Posted 5 years ago #

    Maybe something like this.

    Replace this:

    <?php $current_tag = single_tag_title(); ?>

    With this:

    <?php
    	global $wp_query;
    	$tag = $wp_query->get_queried_object();
    	$current_tag = $tag->term_id;
    ?>
  3. MichaelH
    Member
    Posted 5 years ago #

    This should work with the WordPress Default Theme's index.php:

    <?php
    //for use in the loop, list 5 post titles related to first tag on current post
    $tags = wp_get_post_tags($post->ID);
    if ($tags) {
      echo 'Related Posts';
      $first_tag = $tags[0]->term_id;
      $args=array(
        'tag__in' => array($first_tag),
        'post__not_in' => array($post->ID),
        'showposts'=>5,
        'caller_get_posts'=>1
       );
      $my_query = new WP_Query($args);
      if( $my_query->have_posts() ) {
        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;
      }
    }
    ?>
  4. patcheta
    Member
    Posted 5 years ago #

    Thanks so much MichaelH, I can't tell you how much I appreciate your help. Was that information listed in a doc somewhere? If so I missed it. Please post the link if there is one. Thanks again.

  5. MichaelH
    Member
    Posted 5 years ago #

    No, just coded that for you.

  6. Justin Tadlock
    Member
    Posted 5 years ago #

    Ahh! I just realized what I coded was for tag archive pages. Where's my head at? :)

  7. MichaelH
    Member
    Posted 5 years ago #

    @greenshady: well you've got a million other good ideas and code examples, Justin...

    patcheta: saw something yesterday that should probably be changed/added at the end of the code there, adding the wp_reset_query so it doesn't blowup the_post in your regular loop.

    <?php
    //for use in the loop, list 5 post titles related to first tag on current post
    $tags = wp_get_post_tags($post->ID);
    if ($tags) {
      echo 'Related Posts';
      $first_tag = $tags[0]->term_id;
      $args=array(
        'tag__in' => array($first_tag),
        'post__not_in' => array($post->ID),
        'showposts'=>5,
        'caller_get_posts'=>1
       );
      $my_query = new WP_Query($args);
      if( $my_query->have_posts() ) {
        while ($my_query->have_posts()) : $my_query->the_post(); ?>
          <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>
    
          <?php
        endwhile;
      }
    wp_reset_query();
    }
    ?>
  8. patcheta
    Member
    Posted 5 years ago #

    You guys are awesome. Thanks so much!

  9. EJU
    Member
    Posted 5 years ago #

    hi, can i request to you the same for Recent Post frome the same category? I am working with this code outside the loop (only category but goes with author too), but yours seem very better.

    <?php

    //Gets category and author info
    global $wp_query;
    $cats = get_the_category();
    $postAuthor = $wp_query->post->post_author;
    $tempQuery = $wp_query;
    $currentId = $post->ID;

    // related author posts
    $newQuery = "posts_per_page=5&author=" . $authorPosts;
    query_posts( $newQuery );
    $authorPosts = "";
    $count = 0;
    if (have_posts()) {
    while (have_posts()) {
    $count++;
    the_post();
    if( $count<4 && $currentId!=$post->ID) {
    $count++;
    $authorPosts .= '

  10. ' . the_title( "", "", false ) . '
  11. ';
    }
    }
    }

    // related category posts
    $catlist = "";
    forEach( $cats as $c ) {
    if( $catlist != "" ) { $catlist .= ","; }
    $catlist .= $c->cat_ID;
    }
    $newQuery = "posts_per_page=5&cat=" . $catlist;
    query_posts( $newQuery );
    $categoryPosts = "";
    $count = 0;
    if (have_posts()) {
    while (have_posts()) {
    the_post();
    if( $count<4 && $currentId!=$post->ID) {
    $count++;
    $categoryPosts .= '

  12. ' . the_title( "", "", false ) . '
  13. ';
    }
    }
    }
    $wp_query = $tempQuery;
    ?>
    <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
    <h2>Ultimos artículos de la categoría: <?php the_category(', ');?></h2>

    • <?php echo $categoryPosts; ?>

    <?php endwhile;?>

  • wrosner
    Member
    Posted 5 years ago #

    I have been looking high and low for an answer to my problem which is related to above.

    In my template I am displaying a page and wanting to show groups of related posts on the right. Each group is a category and each post is also tagged with a keyword. In this particular part of my site I am using the title of the page as the match for the keyword. Example might be category of "Quick Tips" for tag equaling "Foo". I would want that post listed when on the "Foo" page.

    Here's the code.

    <?php
    			$args ="cat=80&amp;tag=".$mypage_title."&amp;showposts=5";
    			echo ($args);
    			$recent = new WP_Query($args);
    
    		if ($recent->have_posts()) {
    			?><h4>Quick Tips</h4>
    			<ul>
    			<?php
    			while($recent->have_posts()) : $recent->the_post();
    				//if (has_tag($mypage_title)) {
    				if (get_post_meta($post->ID, 'flv', true)) {
    				?>
    				<li><a class="sidebarnav1" href="<?php the_permalink()?>"><img width="20" src="/wordpress/wp-content/themes/revolution_business-10/_images/video2.jpeg"> <?php the_title(); ?></a></li>
    				<?php }else{ ?>
    				<li><a class="sidebarnav1" href="<?php the_permalink()?>"><?php the_title(); ?></a></li>
    
    				<?php }//}
    			endwhile; ?>
    			</ul>

    When I use add the "tag=????" I stopped getting back any posts. I have echo'd my $args and they do contain the page title. I got page title using

    $mypage_title = trim(wp_title('',false,''));

    What am I doing wrong that combining category and tag won't work?

    Thanks for any help!

    Wes

  • Hoboballan
    Member
    Posted 5 years ago #

    Hi MichaelH,

    This code is great, but for some reason if there is a comment for the last relevant post, it puts that into comments section..?

    Is there something i'm missing here?

  • foglub
    Member
    Posted 5 years ago #

    Hey Michael H,

    Is there any way to change the code you made from 'first tag' to display 5 posts related (randomly) to ALL of the current post's tags?

    Thanks!

  • mbuhnici
    Member
    Posted 5 years ago #

    @MichaelH
    I have the same problem with the code, the comments displayed come from the last post in the similar posts list. What can I do about it?

  • mbuhnici
    Member
    Posted 5 years ago #

    I found the solution myself. Here: http://www.3mind.at/2009/05/06/code-highlighting/

    Backup the current $post object at the beginning of our code and copy it back at the end, where you also call the built-in function wp_reset_query().

    <?php //for use in the loop, list 5 post titles related to first tag on current post
    $backup = $post; // backup the current object
    $tags = wp_get_post_tags($post->ID);
    $tagIDs = array();
    if ($tags) {
    $tagcount = count($tags);
    for ($i = 0; $i < $tagcount; $i++) {
    $tagIDs[$i] = $tags[$i]->term_id;
    }
    $args=array(
    'tag__in' => $tagIDs,
    'post__not_in' => array($post->ID),
    'showposts'=>5,
    'caller_get_posts'=>1
    );
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
    <h3>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></h3>
    <?php endwhile;
    }
    }
    $post = $backup; // copy it back
    wp_reset_query(); // to use the original query again
    ?>

  • Topic Closed

    This topic has been closed to new replies.

    About this Topic

    Tags