Advertisement
Guest User

SQL to grab posts by coauthor user ID

a guest
Mar 10th, 2015
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.97 KB | None | 0 0
  1. <?php
  2.  
  3.     /**
  4.      * Create an SQL statement for searching posts by coauthor name.
  5.      *
  6.      * @todo I'm having trouble getting $wpdb -> prepare to work, although I don't think it's necessary since I am sanitizing the data very strictly.
  7.      * @todo I really have no idea if this is actually how you do a JOIN.
  8.      *
  9.      * @param string $s The search term.
  10.      * @param string $matching_users A comma-sep list of user ID's.
  11.      * @param int $limit The maximum number of results.
  12.      * @param int $offset The number of results to skip, as per pagination.
  13.      * @return string SQL for searching posts by coauthor name.
  14.      */
  15.     private function sql( $s, $matching_users, $limit, $offset ) {
  16.        
  17.         // Sanitize the search term and matching users, although replacing illegal chars with a wildcard.
  18.         $s              = $this -> sanitize( $s, '_' );
  19.         $matching_users = $this -> sanitize( $matching_users, '_' );
  20.  
  21.         // And then hit them with esc_sql because I'm paranoid.
  22.         $s              = esc_sql( $s );
  23.         $matching_users = esc_sql( $matching_users );
  24.  
  25.         // Sanitize the LIMIT making sure it's an int, defaulting to 10.
  26.         $limit = absint( $limit );
  27.         if( empty ( $limit ) ) { $limit = 10; }
  28.  
  29.         // Sanitize the the OFFSET, making sure it's an int, defaulting to 0.
  30.         $offset = absint( $offset );
  31.         if( empty ( $offset ) ) { $offset = 0; }
  32.  
  33.         // We're gonna need this to build our table names.
  34.         global $wpdb;
  35.  
  36.         // Shorten the name of the psots table for easier reading.
  37.         $p  = $wpdb -> posts;
  38.         $tt = $wpdb -> term_taxonomy;
  39.         $tr = $wpdb -> term_relationships;
  40.         $t  = $wpdb -> terms;
  41.        
  42.         $sql = <<<SQL
  43.  
  44.             /* We're selecting posts in order to fulfill the search query. */
  45.             SELECT
  46.  
  47.             /* This will prevent multiple copies of the same post, though I don't totally understand why we'd otherwise be getting them. */
  48.             DISTINCT
  49.  
  50.             /* Populates a value so you can determine how many rows you would have gotten without the LIMIT clause. */
  51.             SQL_CALC_FOUND_ROWS
  52.  
  53.             /* We want every column from the posts table. */
  54.             $p.*
  55.  
  56.             /* I want results from the posts table, but I have to invoke these other tables for when I am sort of using them later in WHERE clause.  */
  57.             FROM $p, $tr, $t, $tt
  58.  
  59.             WHERE (
  60.  
  61.                 /* See if the search term matches the post title. */
  62.                 (
  63.                     $p.post_title LIKE '%$s%'
  64.                
  65.                 /* See if the search term matches the post content. */
  66.                 ) OR (
  67.                     $p.post_content LIKE '%$s%'
  68.  
  69.                 /* See if the post author is in the array of author ID's that I have from my script. */
  70.                 ) OR (
  71.                     $p.post_author IN ( $matching_users )
  72.                
  73.                 /* This is the part where I'm hazy. */
  74.                 ) OR (
  75.                    
  76.                     /* Search the TT table for terms from the 'author' taxonomy ...  */
  77.                     (
  78.                         $tt.taxonomy = 'author'
  79.                    
  80.                     /* ... and whose term description matches the search term ...  */
  81.                     ) AND (
  82.                         $tt.description LIKE '%$s%'
  83.                    
  84.                     /* ... and whose ID matches the ID in the terms table where ...  */
  85.                     ) AND (
  86.                         $tt.term_id = $t.term_id
  87.  
  88.                     /* ... the term_id matches the TR table where ...  */
  89.                     ) AND (
  90.                         $tr.term_taxonomy_id = $t.term_id
  91.  
  92.                     /* ... the object_id in the TR table matches the post ID ... */
  93.                     ) AND (
  94.                         $tr.object_id = $p.ID
  95.                     )
  96.  
  97.                 /* End the part where I'm hazy. */
  98.                 )
  99.  
  100.             /* Limit results to the normal post types. */
  101.             ) AND $p.post_type IN (
  102.                 'post', 'page'
  103.  
  104.             /* Limit results to the normal post status. */
  105.             ) AND (
  106.                 $p.post_status = 'publish'
  107.            
  108.             /* WP Core does this and I have no idea why. */
  109.             ) AND (
  110.                 1 = 1
  111.             )
  112.  
  113.             /* Get results in reverse cron, like usual. */
  114.             ORDER BY $p.post_date DESC
  115.        
  116.             /* Of course we only want our normal posts per page. */
  117.             LIMIT $limit
  118.  
  119.             /* And we need to account for pagination. */
  120.             OFFSET $offset
  121.  
  122. SQL;
  123.  
  124.         return $sql;
  125.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement