• I am trying to make a code that looks at the post’s tag, and then finds posts with the same tags and displays the titles of those posts. This is so that it can display “RELATED POSTS”.

    <?php
    $posttags = get_the_tags();
    if ($posttags) {
    foreach($posttags as $tag) {
    global $post;
    print $tag->name . ‘ ‘;
    $tagged=$tag->name . ‘ ‘;
    print $tagged;
    $myposts = get_posts(‘tag=’$tagged);
    foreach($myposts as $post) { ?>

    • “><?php the_title(); ?>
    • <?php } ?>
      <?php } ?>
      <?php } ?>

      but it doesn’t work. it prints out the tag name, so i know it’s definitely getting it inside the variables, and if i replace the get_posts variable for the actual string, it finds the posts so the functions are working. but together, its not actually giving me the results.

      Can someone help me with this? Or is there an easier way to do this?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Anonymous User

    (@anonymized-473288)

    This bit of code seems to work. Not sure if it is the best way.

    $tag_to_post = "'tag=" . $tag->name . "'";
     global $post;
     $postslist = get_posts($tag__to_post);

    You will have to write the other code around it. ??

    You could check out the YARPP Yet Another Related Posts Plugin:
    https://www.ads-software.com/extend/plugins/yet-another-related-posts-plugin/

    As well as a number of other plugins. Plugins can be found on this website at ‘Extend’ at the top right of the screen and then ‘Plugins’ in the left menu.

    If you do a search for ‘related posts’ (without the quotation marks) you get the following results:
    https://www.ads-software.com/extend/plugins/search.php?q=related+posts

    Anonymous User

    (@anonymized-473288)

    Update:
    My bad. That should be $tag->slug according to:
    https://codex.www.ads-software.com/Template_Tags/get_posts

    $tag_to_post = "'tag=" . $tag->slug . "'";
     global $post;
     $postslist = get_posts($tag__to_post);

    In retrospect it appears not be working either way.
    Not sure what is going on.

    Anonymous User

    (@anonymized-473288)

    Aha. It appears that the variable for the tags needs to be in an array. ??

    So something like this, which appears to work:

    <?
    $posttags = get_the_tags($post->ID);
    if ($posttags) {
    /*Define counter*/
    $counter = 0;
      foreach($posttags as $tag) {
      /*Add to counter*/
      $counter++;
       $tag_post = $tag->name;
       echo '<a href="';
       echo get_tag_link($tag);
       echo '">';
       echo $tag_post;
       echo '</a>';
     ?>
    <ul>
    <?
    $args=array(
       'tag'=>$tag_post,
       'orderby'=>date,
       'order' => asc,
         );
    $my_query = new WP_Query($args); 
    
    while ($my_query->have_posts()) : $my_query->the_post(); ?>
     <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
    </ul>
    <?
      }
    }
    ?>

    If you want to list all the posts under one heading you will have to rewrite the code. I am using a ‘wp_query’. I don’t know if you could use a ‘for each’ loop.
    See also ‘tag parameters’ for ‘query_posts’ in the codex:
    https://codex.www.ads-software.com/Template_Tags/query_posts#Tag_Parameters

    Unfortunately I don’t know enough about this subject to write the perfectly optimized code.

    Please feel free to post any improvements.

    The counter ‘for each’ loop with thanks to the people in this topic:
    https://www.ads-software.com/support/topic/276635?replies=4

    Here’s a slightly cleaner version for you… just cleaned up what you posted a little…

    Not sure why you have the counter, are you using it further down in the code somewhere that you havn’t posted above?.. if not, then it’s just counting for the sake of counting, but not actually doing anything..

    Anyways, here you go..

    <?php
    $posttags = get_the_tags($post->ID);
    if ($posttags) {
    ?>
    	<ul>
    	<?php
    	foreach($posttags as $tag) {
    		?>
    		<li>
    			<a href="<?php echo get_tag_link($tag);?>"><?php echo $tag->name; ?></a><br />
    			<?php
    			$args = array(
    				'tag' => $tag->name,
    				'orderby' => 'date',
    				'order' => 'asc'
    				// Last array item should not have a comma on the end
    			);
    			unset($tag);
    			$my_query = new WP_Query();
    			$my_query->query($args); // Equivalent of query_posts()
    			while ($my_query->have_posts()) : $my_query->the_post();
    			?>
    					<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br />
    			<?php
    			endwhile;
    			?>
    		</li>
    		<?php
    	}
    	?>
    	</ul>
    	<?php
    }
    ?>

    One list instead of several, and a little less use of un-needed variables..

    Hope that helps.. ??

    p.s. sorry about the tabbing in the code, the forum seems to blow my tabbing into huge proportions, my indentation is not that big in the editor…

    Anonymous User

    (@anonymized-473288)

    Thanx for taking the time to look at the code. ??

    One thing I noticed is that you have <br /> at the end of your href tags within the li tags. I am not sure, but I seem to remember this is against W3C specs.

    The counter was part of the original code at the link at the bottom of my previous post. As I was not certain what it did, I thought it best to leave it in there until the code is tweaked to perfection.

    As for the number of lists, that is because I am working on this bit of code which will (when it is finished) create tabs automatically based on the number of tags:
    https://www.ads-software.com/support/topic/219641?replies=3

    Hadn’t figured out how to get the posts inside the tabs, until I ran into this topic. Considering the poster had a similar question, I posted the code that I did find, and was able to piece together so far. Up to anyone else to customize it as needed. ??

    Thanks again for posting the code. Much appreciated. ??

    Well to fix the invalidation (if it is, i don’t know personally), you could just add a class to those links, then set that given class to display:block, you’d then get the appearance of new lines without the need to actually write them in… ??

    Although i’m pretty sure the above can be refined…

    Displaying the titles linking to posts, and using a complete query to the posts table is inefficient… because you’re essentially grabbing all the data on those posts while only displaying/using 2 pieces of information from that data, however this isn’t uncommon and i see this alot here and on WordPress guides… unless WordPress does some special data flushing after queries then this data is just floating around being unused unless it’s cleared/unset..

    How much of a difference does it make? … no idea, it could be marginable, and not even worth a mention… but i’m not hugely concerned enough to benchmark the differences or hunt pages of code to refine….

    “If it’s not broken” …

    Anonymous User

    (@anonymized-473288)

    Well to fix the invalidation (if it is, i don’t know personally), you could just add a class to those links, then set that given class to display:block, you’d then get the appearance of new lines without the need to actually write them in… ??

    According to W3C specs, as far as I know, and please correct me if I am wrong, this might be incorrect. I believe, as I understand, the proper way might be to go for a nested list or definition list in such cases. But I am not a 100% sure. As far as I understood lists should be used for just lists, and not paragraph style or multiple lines in one go that have a different function. From an accessibility point of view it might be confusing as well. And in some countries accessible design is being made, if it isn’t already, obligated by law. Which is a good thing, as it benefits all. W3C and search engines are keen on accessible design as well. I forgot to add it last time, but the links should have a title tag, which might cancel the need for using a nested list or definition list. I am not sure though about that last part though, or if one should want to go down that road… So far I have found accessibility design has actually made my life as a designer easier.

    Displaying the titles linking to posts, and using a complete query to the posts table is inefficient… because you’re essentially grabbing all the data on those posts while only displaying/using 2 pieces of information from that data, however this isn’t uncommon and i see this alot here and on WordPress guides… unless WordPress does some special data flushing after queries then this data is just floating around being unused unless it’s cleared/unset..

    Honestly, I haven’t seen enough code written for WordPress to know about that. From a personal programming ethics point of view I would go for the optimized version, keeping database and server load to a minimum. Like I said the code I am writing is code in development. However caching can in this case can eliminate (to some extent) the need to optimize the code, but it would however be ugly code writing in my opinion. But yeah definitely, running for example 100 queries for a 100 tags per post could possibly definitely affect the performance to the point that it could be detrimental (depending on the server setup of course). This is general and not WordPress related. Caching should be able to catch this (to some degree) and save server load on multiple views of the same post, but like I said it depends on the use case whether or not this is a feasible option. There might be better ways to do it, such as rewinding the posts and using the original query.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Variables through get_posts? query_posts?’ is closed to new replies.