logo
Ask your WordPress questions! Pay money and get answers fast! (more info)

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.

$30
List posts from specific tags

This question goes regarding a calendar tutorial of this page:

I'm trying to list calendar posts from a specific tag on a regular frontage (index.php or frontage.php) in this tutorial with no luck. Let's say it should list posts only with the tags of towns like "boston" and "newyork" . I figure there must be a code that starts with something like AND +boston AND + newyork(or their ID, it doesn't matter just as long it lists the posts of the tags)... after the row in the section with:

else:
$querystr = "
SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id";



CODE:



<?php
/**
* Do we need to filter by event tag?
*/
if(is_tax('event_tags') ) :
$tag = strip_tags( get_query_var('event_tags') );

$querystr = "
SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta, $wpdb->terms wterms, $wpdb->term_taxonomy wtax, $wpdb->term_relationships wrels
WHERE wposts.ID = wpostmeta.post_id
AND wterms.term_id = wtax.term_id
AND wtax.term_taxonomy_id = wrels.term_taxonomy_id
AND wrels.object_id = wposts.ID
AND wterms.slug = '$tag'
";

else:
$querystr = "
SELECT wposts.*
FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
WHERE wposts.ID = wpostmeta.post_id";
endif;

// Build the rest of the query, i.e. only get events with dates, and order newest first.
$querystr .= "
AND wpostmeta.meta_key = 'Date'
AND STR_TO_DATE(wpostmeta.meta_value,'%m/%d/%Y') >= CURDATE()
AND wposts.post_status = 'publish'
AND wposts.post_type = 'events'
ORDER BY STR_TO_DATE(wpostmeta.meta_value,'%m/%d/%Y') ASC
LIMIT 20
";

This question has been answered.

hlx5 | 12/09/11 at 12:01pm Edit


(4) Possible Answers Submitted...

See a chronological view of answers?

Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

  • avatar
    Last edited:
    12/09/11
    12:15pm
    Clifford P says:

    Please clarify:

    1) Is your CODE currently working properly as-is?
    2) But you want to add some functionality?

    I'm unclear what functionality you're trying to add. Are you saying that you have calendar events somewhere, with each event having tags like 'boston', 'sports', 'birthdays', and 'webinars'?
    And you want to display all the calendar events tagged 'sports' on a page?

    If yes, are the EVENTS a custom post type or how are they being assigned tags?

    Is this all WP Core or some events plugin?

    • 12/09/11 3:39pm

      hlx5 says:

      I just want to show one specific town. Let's say "Boston".
      As it is now, the code shows all towns. Posts with Boston and New York.

      The tags where typed in as "tags" in the admin page of the post. There is now three posts. One post with the tag "Boston". Two posts with "New York". It seems like it is done into taxonomies(the tags).

      See the attached image below....

      Attached Image

    • 12/09/11 3:43pm

      hlx5 says:

      Another image from the adminpage of the post(event)
      See image below

      Attached Image

  • avatar
    Last edited:
    12/09/11
    12:16pm
    John Cotton says:

    I haven't read that tutorial in detail, but it's odd that they are not using WQ_Query or query posts to get things back.

    It's not clear from your question whether towns are custom fields or a taxonomy. Assuming they are the latter (which is probably the better approach) then this might work:


    $args = array(
    'post_type' => 'events', // if you are using a different name for you custom post type, change this!
    'tax_query' => array(
    array(
    'taxonomy' => 'town',
    'field' => 'slug',
    'terms' => 'boston'
    )
    )
    );
    $query = new WP_Query( $args );

    Previous versions of this answer: 12/09/11 at 12:16pm

    • 12/09/11 3:46pm

      hlx5 says:

      It seems to be a taxonomy.

      Attached Image

    • 12/09/11 3:46pm

      hlx5 says:

      Attached Image

    • 12/09/11 6:18pm

      John Cotton says:

      It seems to be a taxonomy.


      So my code will do the trick...have you tried it?

    • 12/09/11 6:49pm

      hlx5 says:

      I tried to do this with your code but with no luck...any suggestions?


      <div id="container">
      <div id="content" role="main">

      <?php


      $args = array(

      'post_type' => 'events', // if you are using a different name for you custom post type, change this!

      'tax_query' => array(

      array(

      'taxonomy' => 'town',

      'field' => 'slug',

      'terms' => 'boston'

      )

      )

      );






      /**
      * Do we need to filter by event tag?
      */
      if(is_tax('calendar_tags') ) :
      $tag = strip_tags( get_query_var('calendar_tags') );

      $querystr = "
      SELECT wposts.*
      FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta, $wpdb->terms wterms, $wpdb->term_taxonomy wtax, $wpdb->term_relationships wrels
      WHERE wposts.ID = wpostmeta.post_id
      AND wterms.term_id = wtax.term_id
      AND wtax.term_taxonomy_id = wrels.term_taxonomy_id
      AND wrels.object_id = wposts.ID
      AND wterms.slug = '$tag'
      ";

      else:

      $query = new WP_Query( $args );


      endif;

      // Build the rest of the query, i.e. only get events with dates, and order newest first.
      $querystr .= "
      AND wpostmeta.meta_key = 'Date'
      AND STR_TO_DATE(wpostmeta.meta_value,'%m/%d/%Y') >= CURDATE()
      AND wposts.post_status = 'publish'
      AND wposts.post_type = 'calendar'
      ORDER BY STR_TO_DATE(wpostmeta.meta_value,'%m/%d/%Y') ASC
      LIMIT 3
      ";

      $calendar = $wpdb->get_results($querystr, OBJECT);

      if ($calendar):
      echo '<ul>';
      foreach ($calendar as $post):
      global $post;
      setup_postdata($post);

      // Get a friendlier version of the day.
      $dateday = get_post_meta($post->ID, 'Date', true);
      $dateday = date_create($dateday);
      $dateday = date_format($dateday, 'j');
      // Get a friendlier version of the month.
      $datemonth = get_post_meta($post->ID, 'Date', true);
      $datemonth = date_create($datemonth);
      $datemonth = date_format($datemonth, 'M');
      ?>
      <li>
      <div class="num"><a href="<?php the_permalink() ?>" title="Link to <?php the_title_attribute(); ?>"><span><?php echo $dateday; ?></span><?php echo $datemonth; ?></a></div>
      <div class="calendarlink float-l">
      <a href="<?php the_permalink() ?>" title="Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>

      </div></li><?php endforeach;
      echo '';
      endif; ?></ul>

      </div>











      <div class="container-read-more"><a href="<?php get_bloginfo('wpurl'); ?>/calendar/" >+ More events</a></div>

      <?php
      /* Run the loop to output the posts.
      * If you want to overload this in a child theme then include a file
      * called loop-index.php and that will be used instead.
      */
      get_template_part( 'loop', 'index' );
      ?>
      </div><!-- #content -->
      </div><!-- #container -->

      <?php get_sidebar(); ?>
      <?php get_footer(); ?>

  • avatar
    Last edited:
    12/09/11
    12:57pm
    Hai Bui says:

    With the code from that tutorial, you can only get the posts for only one town. And it's weird they are not using WQ_Query or query posts.
    In order to help you, please let us know how the url looks like, is it like this?
    http://www.url.com/?town=boston+newyork

    • 12/09/11 3:29pm

      hlx5 says:

      It's fine if it is just from one town.
      The thought is that the list is only going to show posts with the tag "boston" on the frontpage. With the current code all towns show up.

      Attached Image

    • 12/09/11 3:32pm

      hlx5 says:

      This is the code in my index.php

        <?php



      /**
      * Do we need to filter by event tag?
      */
      if(is_tax('calendar_tags') ) :
      $tag = strip_tags( get_query_var('calendar_tags') );

      $querystr = "
      SELECT wposts.*
      FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta, $wpdb->terms wterms, $wpdb->term_taxonomy wtax, $wpdb->term_relationships wrels
      WHERE wposts.ID = wpostmeta.post_id
      AND wterms.term_id = wtax.term_id
      AND wtax.term_taxonomy_id = wrels.term_taxonomy_id
      AND wrels.object_id = wposts.ID
      AND wterms.slug = '$tag'
      ";

      else:
      $querystr = "
      SELECT wposts.*
      FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
      WHERE wposts.ID = wpostmeta.post_id
      ";

      endif;

      // Build the rest of the query, i.e. only get events with dates, and order newest first.
      $querystr .= "
      AND wpostmeta.meta_key = 'Date'
      AND STR_TO_DATE(wpostmeta.meta_value,'%m/%d/%Y') >= CURDATE()
      AND wposts.post_status = 'publish'
      AND wposts.post_type = 'calendar'
      ORDER BY STR_TO_DATE(wpostmeta.meta_value,'%m/%d/%Y') ASC
      LIMIT 3
      ";

      $calendar = $wpdb->get_results($querystr, OBJECT);

      if ($calendar):
      echo '<ul>';
      foreach ($calendar as $post):
      global $post;
      setup_postdata($post);

      // Get a friendlier version of the day.
      $dateday = get_post_meta($post->ID, 'Date', true);
      $dateday = date_create($dateday);
      $dateday = date_format($dateday, 'j');
      // Get a friendlier version of the month.
      $datemonth = get_post_meta($post->ID, 'Date', true);
      $datemonth = date_create($datemonth);
      $datemonth = date_format($datemonth, 'M');
      ?>
      <li>
      <div class="num"><a href="<?php the_permalink() ?>" title="Link to <?php the_title_attribute(); ?>"><span><?php echo $dateday; ?></span><?php echo $datemonth; ?></a></div>
      <div class="calendarlink float-l">
      <a href="<?php the_permalink() ?>" title="Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a>

      </div></li><?php endforeach;
      echo '';
      endif; ?></ul>

    • 12/09/11 3:43pm

      hlx5 says:

      Another image from the adminpage of the post(event)
      See image below

      Attached Image

    • 12/10/11 4:04am

      Hai Bui says:

      If you want to list only posts with the tag "boston" on the frontpage, try to replace:

      if(is_tax('calendar_tags') ) :

      $tag = strip_tags( get_query_var('calendar_tags') );


      with

      if(is_tax('calendar_tags') ) :

      $tag = 'boston';

    • 12/10/11 10:52am

      hlx5 says:

      Tried that but no luck...

    • 12/10/11 11:06am

      hlx5 says:

      But hey, this works and its very close to your answer! Thank you

      /**
      * Do we need to filter by event tag?
      */
      if(is_tax('calendar_tags') ) :
      $tag = strip_tags( get_query_var('calendar_tags') );

      $querystr = "
      SELECT wposts.*
      FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta, $wpdb->terms wterms, $wpdb->term_taxonomy wtax, $wpdb->term_relationships wrels
      WHERE wposts.ID = wpostmeta.post_id
      AND wterms.term_id = wtax.term_id
      AND wtax.term_taxonomy_id = wrels.term_taxonomy_id
      AND wrels.object_id = wposts.ID
      AND wterms.slug = '$tag'
      ";

      else:
      $querystr = "
      SELECT wposts.*
      FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta, $wpdb->terms wterms, $wpdb->term_taxonomy wtax, $wpdb->term_relationships wrels
      WHERE wposts.ID = wpostmeta.post_id
      AND wterms.term_id = wtax.term_id
      AND wtax.term_taxonomy_id = wrels.term_taxonomy_id
      AND wrels.object_id = wposts.ID
      AND wterms.slug = 'boston'
      ";

      endif;

  • avatar
    Last edited:
    12/09/11
    1:25pm
    Luis Abarca says:

    Why not use WP_Query with tax_query, meta_query and posts_where, posts_orderby filters instead of the ugly SQL ?

    Orderby filter
    http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_orderby

    Where filter
    http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_where

This question has expired.



Gabriel Reguly, Francisco Javier Carazo Gil, hlx5 voted on this question.



Current status of this question: Completed



Warning: Please do not give out any FTP or ssh credentials to anyone, unless you trust them completely. Giving out login details is dangerous.

If the asker does not get an answer then they have 10 days to request a refund.