I have 200 tags i am building movie directory website some tag as example

The Wolf of Wall Street (2013)
Goodfellas (1990)
Catch Me If You Can (2002)
Monster (2003)
The Bling Ring (2013)
Devil's Knot (2013)
The Frozen Ground (2013)
Public Enemies (2009)
American Gangster (2007)
The Iceman (2012)

my requirements

  1. I want show selected movie tag that start with 'A or any alphabet' when user click on A link code will generate list tag that start with 'A'.

  2. Show list of movies via years as you noted above tag contain years in number so it is possible tag show via year like user click on 2003 a list will generate.

before posting here i searched well all over the net : ( well i find something like

<?php query_posts('tag=$tag'); ?>
<?php  if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

<div class="title">
<?php echo get_the_title ;?>

<?php endwhile; endif ;?>
  • 1
    each movie is a tag, or a post?
    – Caleb
    Commented Sep 16, 2014 at 21:03
  • each movie is tag
    – Yaldram
    Commented Sep 16, 2014 at 21:39
  • 1
    okay. do you want movies that begin with "The" to be filed under "T", or the first letter of the next word? Will years always be the last and only thing in parentheses?
    – Caleb
    Commented Sep 16, 2014 at 21:51

2 Answers 2


From what I can read in your comments (and your follow up question), you are trying to intercept the WHERE clause of your query. The name__like argument you mentioned in your other question won't work for what you are trying to achieve. The clause is built this way by get_terms() (the underlying function of get_tags()):

if ( ! empty( $args['name__like'] ) ) {
    $where .= $wpdb->prepare( " AND t.name LIKE %s", '%' . $wpdb->esc_like( $args['name__like'] ) . '%' );

As you can see, it's %YOURTERM%, so it matches not only from the beginning, but from the end as well. To make it short: This is not what you want.

After reading source, it's simple: You need to rebuild the complete query. There's a filter for that:

$clauses = apply_filters( 'terms_clauses', compact( $pieces ), $taxonomies, $args );

Simply add a callback to it and modify your query to $wpdb->esc_like( 'term' ).'%':

add_filter( 'terms_clauses', function( $sql, $taxonomies, $args )
    // rebuild your query here

    return $sql;
}, PHP_INT_MAX -1, 3 );

Because ordering posts by value of tags is not an usual occurrence you can't order loop by tags. You would have to use multiple WP loops to do so (or build custom DB query). So you need a foreach loop for each tag and inside that a loop a WP_Query loop. Something like this:

$tags = get_tags();
foreach ( $tags as $tag ) {

    $query = new WP_Query( array( 'tag' => $tag->term_id, 'orderby' => 'meta_value', 'order' => ASC ) );

    while( $query->have_posts ) {
        // print posts

I did not try this but something like this should work.

  • Ok let me try this for year what i do ? using regex to detect year and get tag list
    – Yaldram
    Commented Sep 16, 2014 at 21:37
  • Well of course, you would need to get year via regex, but there are easier ways to do so. It would be better if you had years separated, in custom fields or tags. I don't know why you didn't used categories if you want to have multiple posts for one movie. There are plugins that will turn tags to categories for you, for instance Taxonomy Converter plugin. Commented Sep 17, 2014 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.