• Resolved filmstarr

    (@filmstarr)


    I just thought I’d post this here as I’ve been having some query compatibility issues with secondary-title and my theme.

    The LEFT JOIN that is made when include_in_search in switched on conflicts with standard meta queries that may be hooked elsewhere. The resulting query looks like this:

    SELECT SQL_CALC_FOUND_ROWS DISTINCT wp_posts.ID
    FROM wp_posts 
    INNER JOIN wp_postmeta
    ON ( wp_posts.ID = wp_postmeta.post_id ) 
    INNER JOIN wp_postmeta AS mt1
    ON ( wp_posts.ID = mt1.post_id )
    LEFT JOIN wp_postmeta
    ON wp_posts.ID = wp_postmeta.post_id
    ...

    , and this causes a “Not unique table/alias: ‘wp_postmeta'” error.

    Updating the following two functions to have a table alias sorts this out:

       function secondary_title_search_join($join) {
          global $wpdb;
    
          if(is_search()) {
             $join .= " LEFT JOIN " . $wpdb->postmeta . " AS stmt1 ON " . $wpdb->posts . ".ID = stmt1.post_id ";
          }
    
          return $join;
       }
       function secondary_title_search_where($where) {
          global $wpdb;
    
          if(is_search()) {
             $where = preg_replace(
                "/\(\s*" . $wpdb->posts . ".post_title\s+LIKE\s*(\'[^\']+\')\s*\)/",
                "(" . $wpdb->posts . ".post_title LIKE $1) OR (stmt1.meta_value LIKE $1)",
                $where
             );
          }
    
          return $where;
       }

    I hope this is helpful and thanks for the awesome plugin!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author thaikolja

    (@thaikolja)

    Hi @filmstarr,

    No, thank YOU for your suggestion. I’ll definitely change that code in the next version.

    By the way, would you be interested in beta-testing the new version? It’s already finished, I just need some more testing to avoid bugs and errors for the 3000+ users that are currently using Secondary Title. If yes, please contact me via kolja AT koljanolte.com. If not, it’s no big deal.

    Thanks again

    Thread Starter filmstarr

    (@filmstarr)

    No problem at all and I’d be more than happy to test the new version, will drop you an email!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘wp_postmeta Query Compatibility’ is closed to new replies.