Support » Plugin: Polylang » Setting post language via custom post form

  • Resolved winnewoerp

    (@joschi81)


    Hi Chouby,

    first of all: thanks for this great plugin. Everything’s fine, works perfect.

    However, there are two details that I don’t manage to solve.

    1st: How do I query all posts that have NO language set plus posts of specific languages?
    I have this:
    $query_string = 'posts_per_page=-1&post_type=post&lang=0';
    All posts are shown.
    With this:
    $query_string = 'posts_per_page=-1&post_type=post';
    No posts are shown.
    When I put lang=0,en,sr, only posts with language set to English or Serbian are shown, but the posts with no language set are not there. I also tried lang=NULL,en,sr, which is not working as well. Any suggestions how to solve this issue?

    2nd: I have a custom post form on a frontend page. How do I add a custom meta field for the Polylang plugin to detect the post’s language? For other custom meta I do it like this:
    add_post_meta($my_post_id, $my_post_meta_key, $my_post_meta_value,true);
    I saw that in the DB’s post_meta table, meta keys called ‘_translations’ exist with a serialized value. My question is now: How do I create this serialized custom post meta value when a post is submitted? And what value do I need to use? Is it pll_current_language()?

    Help is much appreciated!

    Regards
    joschi81

    http://wordpress.org/extend/plugins/polylang/

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter winnewoerp

    (@joschi81)

    I found out after another research, that this thread contains the answer to my second question:
    http://wordpress.org/support/topic/polylang-api-function-to-add-translation
    Will try to make it work and post here when it’s done.

    For the first question, however, I didn’t find any answer yet.

    Thread Starter winnewoerp

    (@joschi81)

    I did it like this (within my functions.php):

    do_action('wp_insert_post', 'wp_insert_post');
    global $polylang;
    $lang = pll_current_language();
    if(method_exists($polylang,'set_post_language')) $polylang->set_post_language($current_post_id, isset($lang) ? $lang : $this->get_preferred_language());

    @chouby: Wouldn’t it be great to hook into the wp_insert_post action and set the language of new posts automatically, even if they are posted via a frontend form?

    Plugin Author Chouby

    (@chouby)

    Polylang currently does not provide a way to query posts with no language. Maybe what you want to reach can be obtained with an advanced query like this (not tested):

    $posts = get_posts(array(
    	'post_type'    => 'post',
    	'tax_query'   => array(
    		'relation' => 'OR',
    		array(
    			'taxonomy' => 'language',
    			'terms'    => get_terms('language', array('fields'=>'ids')),
    			'operator' => 'NOT IN'
    		),
    		array(
    			'taxonomy' => 'language',
    			'field'    => 'slug',
    			'terms'    => 'en'
    		)
    	)
    ));

    Wouldn’t it be great to hook into the wp_insert_post action and set the language of new posts automatically, even if they are posted via a frontend form?

    I did not take this usage into account yet. I will try to look at this for future. I don’t know what will be the consequences as currently Polylang totally separates admin and frontend.

    Thread Starter winnewoerp

    (@joschi81)

    Hi Chouby,

    thank you. Tried it like this, but it doesn’t seem to work:

    $tax_query   = array(
    		'relation' => 'OR',
    		array(
    			'taxonomy' => 'language',
    			'terms'    => get_terms('language', array('fields'=>'ids')),
    			'operator' => 'NOT IN'
    		),
    		array(
    			'taxonomy' => 'language',
    			'field'    => 'slug',
    			'terms'    => 'en'
    		),
    		array(
    			'taxonomy' => 'language',
    			'field'    => 'slug',
    			'terms'    => 'sr'
    		));
    		$query_string =  'posts_per_page=-1&post_type=post&tax_query=$tax_query';

    Any quick idea? Did I get something wrong here?

    Thank you for your info about why you don’t hook into submitted frontend post forms at the moment.

    Plugin Author Chouby

    (@chouby)

    You can’t mix a string with an array. But you can query both languages at the same time:

    $posts = get_posts(array(
    	'post_type'    => 'post',
    	'tax_query'   => array(
    		'relation' => 'OR',
    		array(
    			'taxonomy' => 'language',
    			'terms'    => get_terms('language', array('fields'=>'ids')),
    			'operator' => 'NOT IN'
    		),
    		array(
    			'taxonomy' => 'language',
    			'field'    => 'slug',
    			'terms'    => array('en', 'sr')
    		)
    	)
    ));
    Plugin Author Chouby

    (@chouby)

    Wouldn’t it be great to hook into the wp_insert_post action and set the language of new posts automatically, even if they are posted via a frontend form?

    This will be integrated in Polylang v1.1. You can already test the development version 1.1dev10 (not for production site).
    http://downloads.wordpress.org/plugin/polylang.zip

    Thread Starter winnewoerp

    (@joschi81)

    Thanks Chouby! That’s great news.

    I mark this as resolved now. I didn’t test your code one more time, because finally I had to change my system a bit and didn’t need this solution anymore. But I’m sure that it’s correct. Thanks again!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Setting post language via custom post form’ is closed to new replies.