• Resolved muh

    (@muh)


    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://ww.wp.xz.cn/plugins/xili-language/

Viewing 13 replies - 1 through 13 (of 13 total)
  • Plugin Author Michel – xiligroup dev

    (@michelwppi)

    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

    (@muh)

    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.

    Thread Starter muh

    (@muh)

    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

    (@muh)

    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

    (@michelwppi)

    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

    (@muh)

    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

    (@michelwppi)

    Simon,
    Release 2.12.0 is in tab developers (Other versions Β§) :
    http://ww.wp.xz.cn/plugins/xili-language/developers/
    Thanks for your returns

    M.

    Thread Starter muh

    (@muh)

    Thanks for the update. I just installed it. Seems to work fine.

    Thread Starter muh

    (@muh)

    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

    (@michelwppi)

    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

    (@muh)

    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

    (@michelwppi)

    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. I will reply directly.
    M.

    Thread Starter muh

    (@muh)

    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.