Redfern_89

taxonomy.php

Jul 5th, 2018
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.61 KB | None | 0 0
  1. <?php
  2.  
  3.     function create_sql_taxonomy($taxonomies) {
  4.         $t_count = 1;
  5.         $sql = [];
  6.         $sql_str = '';
  7.         $terms_ = [];
  8.        
  9.         for ($i = 0; $i < count($taxonomies); $i++) {
  10.             for ($j = 0; $j < count($taxonomies[$i]); $j++) {
  11.                
  12.                 $t = $taxonomies[$i][$j];
  13.                
  14.                 switch ($j) {
  15.                     case 0:
  16.                         $logic = $t;
  17.                         break;
  18.                     case 1:
  19.                         $taxonomy = $t;
  20.                         break;
  21.                     case 2:
  22.                         $terms = $t;
  23.                         break;
  24.                 }
  25.             }
  26.            
  27.             if ($logic == 'AND') {
  28.                 for ($x = 0; $x < count($terms); $x++) {
  29.                     $term = $terms[$x];
  30.                     $sql_str = "JOIN ex_term_relationships AS tr$t_count ON tr$t_count.object_id = p.id\r\n";
  31.                     $sql_str .= "JOIN ex_term_taxonomy AS tt$t_count ON tt$t_count.term_id = tr$t_count.term_taxonomy_id\r\n";
  32.                     $sql_str .= "JOIN ex_terms AS t$t_count ON t$t_count.id = tt$t_count.term_id\r\n";
  33.                     $sql_str .= "AND (tt$t_count.taxonomy = '$taxonomy' AND t$t_count.name = '$term')\r\n";
  34.                     $sql[] = $sql_str;
  35.                     $t_count++;
  36.                 }
  37.             }
  38.             if ($logic == 'OR') {
  39.                 for ($x = 0; $x < count($terms); $x++) {
  40.                     $terms_[$x] = "'{$terms[$x]}'";
  41.                 }
  42.                 $sql_str = "JOIN ex_term_relationships AS tr$t_count ON tr$t_count.object_id = p.id\r\n";
  43.                 $sql_str .= "JOIN ex_term_taxonomy AS tt$t_count ON tt$t_count.term_id = tr$t_count.term_taxonomy_id\r\n";
  44.                 $sql_str .= "JOIN ex_terms AS t$t_count ON t$t_count.id = tt$t_count.term_id\r\n";
  45.                 $sql_str .= "AND (tt$t_count.taxonomy = '$taxonomy' AND t$t_count.name IN (" . implode(',', $terms_) . "))\r\n";
  46.                 $sql[] = $sql_str;
  47.                 $t_count++;
  48.             }
  49.            
  50.            
  51.         }
  52.        
  53.         return implode("\r\n", $sql);
  54.        
  55.     }
  56.  
  57. ?>
Advertisement
Add Comment
Please, Sign In to add comment