ivandoric / WordPress-Custom-Query

Repository for Wordpress Custom Query video tutorial series
24 stars 13 forks source link

How to filter data using taxonomy terms? #1

Open athossin opened 7 years ago

athossin commented 7 years ago

During creating team latest result filter, I have the following codes but it does not work. It shows the corrcet url: As for example: If I want to filter the data of 2016-17 season then it shows following url:

http://localhost/wordpress/football_team/barcelona/?season_slugs=2016-17

I watched your youtube video on custom filter where you used meta query and key, but I need filter the data using custom taxonomy ; session, . When I echo out the following codes then it show 2016-17, so its working.But the data is not filtering.

if($_GET['season_slugs'] && !empty($_GET['season_slugs']))
{
   $season_slugs=$_GET['season_slugs'];
   echo $season_slugs;
 }

?>

Following is my code which shows the latest result of all seasons.

<?php 

if($_GET['season_slugs'] && !empty($_GET['season_slugs']))
{
   $season_slugs=$_GET['season_slugs'];
   echo $season_slugs;
 }

?>

<form action="<?php  the_permalink(); ?>" method="get">
  <select name="season_slugs">

    <?php 
     $args = array(
  'type'                     => 'football_team',
  'child_of'                 => 0,
  'parent'                   => '',
  'orderby'                  => 'name',
  'order'                    => 'ASC',
  'hide_empty'               => 1,
  'hierarchical'             => 1,
  'exclude'                  => '',
  'include'                  => '',
  'number'                   => '',
  'taxonomy'                 => 'session',
  'pad_counts'               => false );
$categories = get_categories($args);

foreach ($categories as $category) { ?>
  <option value="<?php echo $category->name; ?>">
      <?php echo $category->name; ?>
  </option>

<?php
}

    ?>

</select>
  <input type="submit">
</form>
<?php

$team = get_post_meta( get_the_ID(), 'football_team_team_name', true );
$season = wp_get_post_terms( $post->ID, 'session' );
$season_slugs = wp_list_pluck( $season, 'slug' );  

$args = array(
  'post_type'  => 'football_fixture',
  'posts_per_page'=>30,
  'meta_key'   => 'pb_match_date',
  'orderby'    => 'meta_value',
  'order'      => 'ASC',
  'tax_query' => array(
          array(
          'taxonomy' => 'session',
          'terms' => $season_slugs,
          'field' => 'slug'
          ), 
        ),
  'meta_query' => array(
    'relation' => 'AND',
    array(
      'key'     => 'pb_match_status',
      'value'   => 'ft'
    ),
                array(
                        'relation' => 'OR',
                        array(
                                'key' => 'match_details_home_team',
                                'value' => $team,
                                'compare' => '='
                        ),
                        array(
                                'key' => 'match_details_away_team',
                                'value' => $team,
                                'compare' => '='
                        ),
    ),
  ),
);

    $my_query = null;
    $my_query = new WP_Query($args); ?>
```
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
  <div class="competition-result clearfix" id="post-<?php the_ID(); ?>">
    <?php
      $id     = get_the_ID();
      $date     = rwmb_meta( 'pb_match_date','', $post->ID);
      $time     = rwmb_meta( 'pb_match_time','', $post->ID );
      $team_home  = get_post_meta( $post->ID, 'match_details_home_team', true );
      $team_away  = get_post_meta( $post->ID, 'match_details_away_team', true );
      $team_home_score  = get_post_meta( get_the_ID(), 'pb_home_score', true );
      $team_away_score  = get_post_meta(get_the_ID(), 'pb_away_score', true );
    ?>
    <div class="competition-league-name-and-date clearfix">
      <div class="competition-league-name-right">
        <?php $terms = get_the_terms( get_the_ID(), 'competition' );
          if ( $terms && ! is_wp_error( $terms ) ){
              foreach ( $terms as $term ) {
                echo "$term->name";
              }
          }
        ?>
      </div>
      <div class="competition-league-name-right">
        <?php $season_slugs= get_the_terms( get_the_ID(), 'session' );
          if ( $season_slugs && ! is_wp_error( $season_slugs ) ){
              foreach ( $season_slugs as $sessions ) {
                echo "$sessions->name";
              }
          }
        ?>
      </div>
      <div class="competition-match-date-left">
        <i class="fa fa-calendar"></i>
        <span> <?php  $dates = new DateTime($date); echo $dates->format('d M Y'); ?>
      </div>
    </div>
    <div class="fixture-details">  
      <aside class="competition-home-team-name-left">
       <a href="<?php echo get_link_by_slug( $team_home ); ?>"> <?php echo $team_home; ?></a>
      </aside>
      <aside class="competition-team-logo-and-time">
        <aside class="football-home-team-logo">
          <img  src="<?php echo get_team_image_from_title($team_home);?>" />
        </aside>
        <aside class="football-match-time">
          <button type="button" class="btn match-time-button"><span><?php echo $team_home_score; ?></span> - <span><?php echo $team_away_score; ?> </span></button>
        </aside>
        <aside class="football-away-team-logo">
          <img  src="<?php echo get_team_image_from_title($team_away);?>" />
        </aside>
      </aside>
      <aside class="competition-away-team-name-right">
        <a href="<?php echo get_link_by_slug( $team_away ); ?>"><?php echo $team_away; ?></a>
      </aside>
      <aside class="football-match-centre-details">
        <div class="football-match-details" data-toggle="tooltip" data-placement="top" title="Click for details">
            <a href="<?php the_permalink(); ?>"><i class="fa fa-arrow-right" aria-hidden="true"></i></a>
        </div>
      </aside>
    </div>  
  </div><!--End post-->     

<?php endwhile; ?> <?php wp_reset_postdata(); ?>

ivandoric commented 7 years ago

Did you try doing just a simple tax_query, without all that other stuff? Just to see if it works?

athossin commented 7 years ago

![Uploading session.png…]() I can control it from admin site using tax query, But how can i control from front side?

On Mon, Nov 14, 2016 at 3:29 PM, Ivan Dorić notifications@github.com wrote:

Did you try doing just a simple tax_query, without all that other stuff? Just to see if it works?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/ivandoric/WordPress-Custom-Query/issues/1#issuecomment-260287320, or mute the thread https://github.com/notifications/unsubscribe-auth/ATtZKThyizFP2uOzC9y3Y848aP0yhbaUks5q-Cn_gaJpZM4KxGMT .

ivandoric commented 7 years ago

What I'm asking is if you tried to do something like this:


$args = array(
  'post_type'  => 'football_fixture',
  'posts_per_page'=>30,
  'tax_query' => array(
          array(
          'taxonomy' => 'session',
          'terms' => $season_slugs,
          'field' => 'slug'
          ), 
        )
)

and then trying to display football_fixtures. Just to see if your tax query is working. So without all the other queries you have in your code.

athossin commented 7 years ago

yes, it is working. but not filtering

ivandoric commented 7 years ago

Did you try doing meta_query before the tax_query?

ivandoric commented 7 years ago

And also did you try doing only meta_query to see if it works. It looks pretty complex.