0

I've setted custom columns for a custom post type, and I want to add a filtering functionality. I need to use custom data, so I can't take advantage from things like wp_dropdown_categories and the like.

My current code:

// add select
function add_filter_by_macroarea() {
    global $typenow;
    $post_type = 'distributor';
    $macroaree = array('italy','france','spain','world');

    if($typenow == $post_type) {
      $html = '<select id="filter-by-macroarea" name="macroarea">';

      foreach($macroaree as $macroarea){
        $html .= '<option value="' . $macroarea . '">' . $macroarea . '</option>';
      }

      $html .= '</select>';
      echo $html;
    };
}

add_action('restrict_manage_posts', 'add_filter_by_macroarea');



// add filtered contents
function convert_id_to_term_in_query($query){
  global $pagenow;
  $post_type = 'distributor';
  $q_vars = &$query->query_vars; // stop, not finished
  var_dump($q_vars); // just to see query vars, not already setted 

} 

add_filter('parse_query', 'convert_id_to_term_in_query');

Now, I'm at the point I need to read a custom GET queryvar, the filter selection. Say it's called 'macroarea' and the values should be 'italy', 'france', 'spain' or'world'.

My issue is: how can I pass such a custom queryvar?

  • 1
    You should be able to access the selected option in $_GET['macroarea'], can you try it? – cybmeta Aug 9 '16 at 15:54
  • Yes! Thank you! I was fooled by thinking I would found that queryvar in the WP_Query object ($query). I'm giung to post all the relevant code for people. – Luca Reghellin Aug 10 '16 at 8:32
  • If you can post as an answer and then accept your own answer, that helps keep the site tidy with your question showing as resolved. Thanks. – Andy Macaulay-Brook Aug 10 '16 at 8:45
1

Thanks to @cybmeta I was able to solve my issue. Below I post all the relevant code. Keep in mind it refers to a 'distributor' custom post type that uses 4 custom taxonomies ('italy','france','spain','world').

// add columns
function add_distributor_columns($table_columns){
  $area_column = array('area' => 'Area');
  $macroarea_column = array('macroarea' => 'Macroarea');
  // put them on the right position
  $table_columns = array_slice($table_columns, 0, 2, true) + $area_column + $macroarea_column + array_slice($table_columns, 2, NULL, true);
  return $table_columns;
}

add_filter('manage_distributor_posts_columns', 'add_distributor_columns', 10);


// add contents to columns
function add_contents_to_distributor_columns($column_name, $id){
  global $post;
  $post_id = $post->ID;

  if($column_name == 'area'){
    $italy_areas = (array) get_the_terms($post_id,'italy');
    $france_areas = (array) get_the_terms($post_id,'france');
    $spain_areas = (array) get_the_terms($post_id,'spain');
    $world_areas = (array) get_the_terms($post_id,'world');

    $areas = $italy_areas + $france_areas + $spain_areas + $world_areas;

    if(!empty($areas) && $areas[0] !== false){
      $areas_names = array();

      foreach($areas as $area){
        $area_name = $area->name;
        $areas_names[] = $area_name;
      }

      echo implode(', ',$areas_names);
    }
  }//end if

  if($column_name == 'macroarea'){
    $macroareas = array();

    if(has_taxonomy('italy', $post_id)) $macroareas[] = 'Italia';
    if(has_taxonomy('france', $post_id)) $macroareas[] = 'Francia';
    if(has_taxonomy('spain', $post_id)) $macroareas[] = 'Spagna';
    if(has_taxonomy('world', $post_id)) $macroareas[] = 'Mondo';

    if(!empty($macroareas)) echo implode(', ',$macroareas);
  }//end if

}// add_contents_to_distributor_columns

add_action('manage_distributor_posts_custom_column', 'add_contents_to_distributor_columns', 10, 2);


// add macroarea filter
function add_filter_by_macroarea() {
    global $typenow;
    $post_type = 'distributor';
    $macroaree = array('all','italy','france','spain','world');
    $selected = isset($_GET['macroarea']) ? $_GET['macroarea'] : '';

    if($typenow == $post_type) {
      $html = '<select id="filter-by-macroarea" name="macroarea">';

      foreach($macroaree as $macroarea){
        $s = ($macroarea == $selected) ? ' selected="selected"' : '';
        $html .= '<option value="' . $macroarea . '"' . $s . '>' . $macroarea . '</option>';
      }

      echo $html .= '</select>';
    };
}

add_action('restrict_manage_posts', 'add_filter_by_macroarea');



// add contents to filtered list
function convert_id_to_term_in_query($query){
  global $pagenow, $post_type;

  if($pagenow == 'edit.php' && $post_type == 'distributor'){
    $selected = isset($_GET['macroarea']) ? $_GET['macroarea'] : null;

    if($selected && $selected != 'all'){
      $terms_ids = wp_list_pluck(get_terms($selected), 'term_id');

      $query->query_vars['tax_query'] = array(
        array(
          'taxonomy' => $selected,
          'field' => 'term_id',
          'terms'    => $terms_ids,
        )
      );
    }// if selected
  }// if right page
}// end filter

add_filter('parse_query', 'convert_id_to_term_in_query');



// UTILITY FUNCTIONS ---------- //

function get_taxonomies_info($post_id = null){
  if(!$post_id){ // if not passed, try to get the ID
    global $wp_query;
    $post_id = $wp_query->get_queried_object_id();//current post id
  }

  global $wpdb;
  return $wpdb->get_results($wpdb->prepare("
      select tax.taxonomy as taxonomy, group_concat(tr.name) as term_names, group_concat(tr.term_id) as term_ids
      from " . $wpdb->prefix . "term_taxonomy tax
      INNER JOIN " . $wpdb->prefix . "term_relationships rel ON tax.term_taxonomy_id =rel.term_taxonomy_id
      INNER JOIN " . $wpdb->prefix . "terms tr ON tr.term_id = tax.term_id
      WHERE rel.object_id = %d
      GROUP BY taxonomy
  ",$post_id),OBJECT_K);
}

function has_taxonomy($taxonomy_slug, $post_id = null){
  if(!$post_id){// if not passed, try to get the ID
    global $wp_query;
    $post_id = $wp_query->get_queried_object_id();//current post id
  }

  return array_key_exists($taxonomy_slug, get_taxonomies_info($post_id));
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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