Title: SQL Error after custom pre_get_posts hook
Last modified: August 21, 2016

---

# SQL Error after custom pre_get_posts hook

 *  Resolved [muh](https://wordpress.org/support/users/muh/)
 * (@muh)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/sql-error-after-custom-pre_get_posts-hook/)
 * I have experienced the following sql error after switched the language (eg. ?
   lang=en_US present in the URL).
 *     ```
       WordPress database error: [Unknown column 'xtt.taxonomy' in 'where clause']
       SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts WHERE 1=1 AND wp_posts.post_type IN ('post', 'somecustomposttype') AND (wp_posts.post_status = 'publish' OR wp_posts.post_author = 1 AND wp_posts.post_status = 'private') AND xtt.taxonomy = 'language' AND xtt.term_id = 5 ORDER BY wp_posts.post_date DESC LIMIT 0, 10
       ```
   
 * After a bit of research in the source I think I’ve found the bug. In the xili-
   language.php:1377 you’re checking if isset the QUETAG in the query_vars but it
   isn’t set to $this->sublang.
 * For the moment I’ve just added
    `$this->sublang = $query_object->query_vars[QUETAG];`
   to the condition and it works fine. The query is generated correctly.
 * I’m not quiet sure if this is the correct solution, but you know. It still works
   😉
 * If I remove my custom pre_get_posts hook (in which I’m just adding a few custom
   post types) it also works.
 * The pre_get_posts hook looks something like this:
 *     ```
       $postTypes = $query->get('post_type');
       if (getType($postTypes) == 'string'){
       	$postTypes = array('post');
       	if (is_page())
       		$postTypes[] = 'page';
       	}
       }
       // $postTypes[] = 'test';
       $query->set('post_type',$postTypes);
       ```
   
 * Greetings
    muh
 * [https://wordpress.org/plugins/xili-language/](https://wordpress.org/plugins/xili-language/)

Viewing 13 replies - 1 through 13 (of 13 total)

 *  Plugin Author [Michel – xiligroup dev](https://wordpress.org/support/users/michelwppi/)
 * (@michelwppi)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/sql-error-after-custom-pre_get_posts-hook/#post-4893286)
 * Thanks for your notes.
 * $this->sublang is reserved in class to detect undefined research (post without
   language) after a test in posts_search_filter()
 * Why do you use this pre_get_posts hook ?
 * Just another – in url a language param is a slug – lowercase – en_us or fr_fr…
 *  Thread Starter [muh](https://wordpress.org/support/users/muh/)
 * (@muh)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/sql-error-after-custom-pre_get_posts-hook/#post-4893290)
 * Because I want to display more than just ‘posts’ on my index page. I’ve added
   multiple different post types that have to be shown.
 * [Detailed Description](http://justintadlock.com/archives/2010/02/02/showing-custom-post-types-on-your-home-blog-page).
 *  Thread Starter [muh](https://wordpress.org/support/users/muh/)
 * (@muh)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/sql-error-after-custom-pre_get_posts-hook/#post-4893299)
 * Ok, you were right. To modify $this->sublang wasn’t that good as I thought.
 * But now im sure I’ve found the bug.
 * xili-language.php:1274
 *     ```
       if ( is_admin () || in_array ( $query_object->query_vars['post_type'], array_keys ($this->authorized_custom_post_type () ) ) )
       	$insert_join = true;
       ```
   
 * You’re assuming that query_vars[‘post_type’] is just a string. But in my case
   it’s an array of strings.
 * Possible solution:
 *     ```
       if (is_admin()){
       	$insert_join = true;
       }else if (is_string($query_object->query_vars['post_type']) && in_array ( $query_object->query_vars['post_type'], array_keys ($this->authorized_custom_post_type () ) )){
       	$insert_join = true;
       }else if (is_array($query_object->query_vars['post_type']) && !count(array_diff($query_object->query_vars['post_type'],array_keys($this->authorized_custom_post_type())))){
       	$insert_join = true;
       }
       ```
   
 * But careful: This will only work if all selected custom post types are authorized.
   As soon as one of them isn’t authorized there will be the same SQL Error.
 * Maybe you want to filter the post types or modify the query to select all of 
   the non authorized posts and only the subset of the authorized.
 *  Thread Starter [muh](https://wordpress.org/support/users/muh/)
 * (@muh)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/sql-error-after-custom-pre_get_posts-hook/#post-4893312)
 * I also modified the _posts\_where\_lang_-Function because of the same problem.
   xlil-language.php:~1564 (the 2.9.2 section):
 *     ```
       $insertWhere = false;
       if ($query_object->query_vars['post_type'] == ''){
       	$insertWhere = true;
       }else if (is_string($query_object->query_vars['post_type']) && in_array ( $query_object->query_vars['post_type'], array_keys ($this->authorized_custom_post_type () ) )){
       	$insertWhere = true;
       }else if (is_array($query_object->query_vars['post_type']) && !count(array_diff($query_object->query_vars['post_type'],array_keys($this->authorized_custom_post_type())))){
       	$insertWhere = true;
       }
       if ($insertWhere){
       	$query_object->query_vars['lang'] = $curlang;
       	$wherereqtag = $this->langs_ids_array[$curlang];
       	$where .= " AND xtt.taxonomy = '" . TAXONAME . "' "; //"
       	$where .= " AND xtt.term_id = $wherereqtag ";
       }
       ```
   
 *  Plugin Author [Michel – xiligroup dev](https://wordpress.org/support/users/michelwppi/)
 * (@michelwppi)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/sql-error-after-custom-pre_get_posts-hook/#post-4893333)
 * Thanks for your rich (and rare) contribution.
    The next release of xili-language(
   2.12) is on the road with renewed parts of code in admin side to include latest
   available functions of WP Core. This particular case with multiple post_types
   mixed in the same multilingual query will be added (first tests are good) – and
   it is an opportunity to optimize codes (some parts are more than 3 years old)
   in these two filters (posts_where, posts_join).
 * Be patient (and ready to test),
 * M.
 *  Thread Starter [muh](https://wordpress.org/support/users/muh/)
 * (@muh)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/sql-error-after-custom-pre_get_posts-hook/#post-4893334)
 * You’re welcome. Thanks for your fast and good feedback.
 * I’m looking forward for the new version.
 * Thanks
    Simon
 *  Plugin Author [Michel – xiligroup dev](https://wordpress.org/support/users/michelwppi/)
 * (@michelwppi)
 * [12 years, 1 month ago](https://wordpress.org/support/topic/sql-error-after-custom-pre_get_posts-hook/#post-4893434)
 * Simon,
    Release 2.12.0 is in tab developers (Other versions §) : [http://wordpress.org/plugins/xili-language/developers/](http://wordpress.org/plugins/xili-language/developers/)
   Thanks for your returns
 * M.
 *  Thread Starter [muh](https://wordpress.org/support/users/muh/)
 * (@muh)
 * [12 years ago](https://wordpress.org/support/topic/sql-error-after-custom-pre_get_posts-hook/#post-4893457)
 * Thanks for the update. I just installed it. Seems to work fine.
 *  Thread Starter [muh](https://wordpress.org/support/users/muh/)
 * (@muh)
 * [12 years ago](https://wordpress.org/support/topic/sql-error-after-custom-pre_get_posts-hook/#post-4893458)
 * I just solved my next issue with the translation of the page. You’re plugin works
   as intended, but.. 😉
 * I don’t know if I broke it or if xili-language just don’t support the translation
   of plugins. That’s why I created a little extension as a separate plugin for 
   xili-language.
 * I’m not very experienced in the internal l10n functions of wp, so I hope you 
   don’t blame me if I did something wrong.
 * Maybe you want to merge it to xili-language or release it as a separate plugin
   or so. I won’t do anything with the code as long as it works. You may change 
   the copyright, I don’t really care. So do as you please, even if you ignore it.
   I just wanted to share this with you:
 *     ```
       class xili_language_plugins {
   
       	protected $oldlang;
       	protected $curlang;
   
       	public function __construct( ) {
       		if (is_admin())
       			return;
       		$this->domains = array();
       		add_action('init', array($this,'loaded'));
       		add_filter('load_textdomain_mofile', array($this,'load_textdomain'),10,2);
       	}
   
       	public function loaded(){
       		if (!isset($GLOBALS['xili_language']))
       			return;
       		$this->xili_lang = $GLOBALS['xili_language'];
   
       		$this->curlang = get_locale();
       		add_filter('locale', array($this,'locale'));
       	}
   
       	public function load_textdomain($moFile, $domain){
       		$this->domains[$domain] = $moFile;
       		return $moFile;
       	}
   
       	protected function replace_text_domains(){
       		foreach ($this->domains as $domain => $moFile){
       			unload_textdomain($domain);
       			load_textdomain($domain, str_replace($this->oldlang, $this->correct_case($this->curlang),$moFile));
       		}
       	}
   
       	protected function correct_case($lang){
       		list($lang,$country) = explode('_',$lang);
       		return strtolower($lang).'_'.strtoupper($country);
       	}
   
       	public function locale($current){
       		if (!isset($this->xili_lang))
       			return $current;
       		if ($this->xili_lang->curlang != $this->curlang){
       			$this->oldlang = $this->curlang;
       			$this->curlang = $this->xili_lang->curlang;
       			$this->replace_text_domains();
       		}
       		return $current;
       	}
   
       } /* **************** end of xili-language-plugin class ******************* */
   
       /****************** instantiation Class *****************/
   
       /**
        * instantiation of xili_language_plugins class
        */
       function xili_language_plugins_start () {
       	global $xili_language_plugin;
       	$xili_language_plugin = new xili_language_plugins( false , false );
       }
       xili_language_plugins_start();
       ```
   
 * and yes, I copied a bit of your code. And sorry for using the name prefix ‘xili’.
 *  Plugin Author [Michel – xiligroup dev](https://wordpress.org/support/users/michelwppi/)
 * (@michelwppi)
 * [12 years ago](https://wordpress.org/support/topic/sql-error-after-custom-pre_get_posts-hook/#post-4893459)
 * xili-language is able to support plugin visitor side translation. The translation
   must be in the local-xx_YY file dedicated to the site.
    please can you contact
   me directly via the support form in 6th tabs of xili-language settings…
 * M.
 *  Thread Starter [muh](https://wordpress.org/support/users/muh/)
 * (@muh)
 * [12 years ago](https://wordpress.org/support/topic/sql-error-after-custom-pre_get_posts-hook/#post-4893461)
 * I think that will be the problem. My plugin translations are not located in the
   local-xx_YY file but in the wp-content/languages/plugins/pluginname-xx_YY.mo 
   files.
 * I’m sorry but I can’t contact you from the support tab. What infos do you need?
 *  Plugin Author [Michel – xiligroup dev](https://wordpress.org/support/users/michelwppi/)
 * (@michelwppi)
 * [12 years ago](https://wordpress.org/support/topic/sql-error-after-custom-pre_get_posts-hook/#post-4893462)
 * I know, on localhost, settings cannot permit support form.
    Some intermediate
   steps of development or conversations between developers/contributors need direct
   contact without forum. So try to use the contact form [http://dev.xiligroup.com/?page_id=10](http://dev.xiligroup.com/?page_id=10).
   I will reply directly. M.
 *  Thread Starter [muh](https://wordpress.org/support/users/muh/)
 * (@muh)
 * [12 years ago](https://wordpress.org/support/topic/sql-error-after-custom-pre_get_posts-hook/#post-4893485)
 * Sorry for the delay. Let put my 2nd issue aside and go back to the first.
 * I wrote a little update to handle post type that aren’t authorized so they will
   be displayed on the page. Otherwise only authorized types with the correct language
   are displayed.
 *     ```
       $notAuthorizedTypes = array();
       if ( empty( $query_object->query_vars['post_type'] ) ) { // string or array
       	$insertWhere = true;
       } else if ( is_string($query_object->query_vars['post_type']) && in_array ( $query_object->query_vars['post_type'], array_keys ( $this->authorized_custom_post_type () ) ) ){
       	$insertWhere = true;
       } else if (is_array($query_object->query_vars['post_type'])){
       	$insertWhere = true;
       	$authorized = $this->authorized_custom_post_type();
       	foreach($query_object->query_vars['post_type'] as $postType){
       		if (!isset($authorized[$postType]) || $authorized[$postType]['multilingual'] != 'enable'){
       			$notAuthorizedTypes[] = $postType;
       		}
       	}
       } else {
       	$insertWhere = false;
       }
       if ( $insertWhere ){
       	$query_object->query_vars[QUETAG] = $curlang; // in query.php posts_join filter is called after posts_where filter - for home page without lang_quetag
       	$need_join = true;
       	$wherereqtag = $this->langs_ids_array[$curlang];
       	$where .= ' AND (';
       	$where .= "(xtt.taxonomy = '" . TAXONAME . "' "; //"
       	$where .= " AND xtt.term_id = $wherereqtag )";
       	if (count($notAuthorizedTypes)){
       		$where .= ' OR ';
       		$where .= 'post_type IN (\''.implode('\', \'', $notAuthorizedTypes).'\')';
       	}
       	$where .= ')';
   
       }
       ```
   
 * **Edit: ** Besides you couldn’t deauthorize post types. Once they were authorized
   they always were. The same problem occures if the post_type index is a string:
   `}
   else if ( is_string($query_object->query_vars['post_type']) && in_array ( $query_object-
   >query_vars['post_type'], array_keys ( $this->authorized_custom_post_type () ))){`
 * You’re just checking if the authorized array contains the post_type but you aren’t
   checking if it’s enabled or not. I think you can fix this yourself. 😉
 * _– Simon_

Viewing 13 replies - 1 through 13 (of 13 total)

The topic ‘SQL Error after custom pre_get_posts hook’ is closed to new replies.

 * ![](https://ps.w.org/xili-language/assets/icon-256x256.png?rev=970404)
 * [xili-language](https://wordpress.org/plugins/xili-language/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/xili-language/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/xili-language/)
 * [Active Topics](https://wordpress.org/support/plugin/xili-language/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/xili-language/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/xili-language/reviews/)

## Tags

 * [filter](https://wordpress.org/support/topic-tag/filter/)
 * [query](https://wordpress.org/support/topic-tag/query/)

 * 13 replies
 * 2 participants
 * Last reply from: [muh](https://wordpress.org/support/users/muh/)
 * Last activity: [12 years ago](https://wordpress.org/support/topic/sql-error-after-custom-pre_get_posts-hook/#post-4893485)
 * Status: resolved