Please make sure to update to WPML 4.3.5 and check our list of Known Issues before reporting

Hi, Amit here, I am the WPML Support Manager, our current ticket queue is high, update your WPML plugins and make sure you meet the minimal requirements for running WPML before reporting an issue please - many tickets are resolved doing that

Please look at our updated list of Known Issues and you can also use our support search to find helpful information and of course review our documentation before opening a ticket.

If you do need to open a ticket please make sure to provide us with all the needed information as described in this page

This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

This topic contains 8 replies, has 3 voices.

Last updated by jin woo 5 years, 10 months ago.

Assigned support staff: Andrey.

Author Posts
January 28, 2014 at 9:39 am #328798

jin woo

I have a function to get ... posts by taxonomy slugs and category slugs.

If I need to get apple category posts. I have fruit taxonomy and apple category.

taxonomy = fruits
category = apple

This is how I get posts of apple category.


<div class="tab-pane active" id="fruit"><?php new main_get_tab("custom-post-type", 10, "post_date", true, "fruits", "full-apple"); ?></div>

This is main_get_tab function


<?php
/*
			echo $term;
		
			$data == icl_object_id($post->ID, $tax, true, 'ko');

			echo "<pre>";var_dump($data);echo "</pre>";

*/
class main_get_tab{
	var $get_posts;
	public function __construct(
		$post_type=NULL, 
		$posts_per_page=5, 
		$orderby="post_date",
		//$suppress_filters=true,
		$suppress_filters=0,
		$tax=NULL,
		$term=Array(),
		$tax_ex=false,
		$post_status="publish"
	){
		$args = Array(
			'suppress_filters' => 0,
			'posts_per_page'   => $posts_per_page,
			'post_status'      => $post_status,
			'orderby'          => $orderby,
			'post_type'        => $post_type
		);
		$args = (($tax != NULL) && ($term != NULL) )? $this->set_category($args, $tax, $term, $tax_ex) : $args;
		//var_dump($args);
		$get_posts = $this->query($args);
		if(!$get_posts) return false;
		$this->output($get_posts);
	}

	function set_category($args, $tax=NULL, $term=Array(), $tax_ex){
		$added = Array(
			'tax_query' => Array(
				Array(
					'taxonomy' => $tax,
					'field' => 'slug',
					'terms' => $term,
					'operator' => (($tax_ex==true)? "NOT IN" : "IN")
				)
			)
		);
		return Array_Merge($args, $added);
	}
	function query($args){
		if(!is_Array($args)) return false;
		return get_posts($args);	
	}

	function output($posts){?>
	<ul>
		<?php
			foreach($posts as $post):?>

			<li class="rick_main_li"><?php echo "<a href='".get_permalink($post->ID)."' target='_blank'>".str_cut($post->post_title, 42)."</a>"; ?></li>
			<!--li><?php echo "<a href='".get_post_url($post->ID)."' target='_blank'>".str_cut($post->post_title, 42)."</a>"; ?></li-->

			<?php endforeach; ?>
	</ul>
		<?php wp_reset_postdata();
	}
};
?>

I added "'suppress_filters' => 0," to get posts by langages.

But I can't really find how to get translated taxonomy slug names.

new main_get_tab("custom-post-type", 10, "post_date", true, "fruits", "full-apple");

All of taxonomies have been translated already. I want to find translated taxonomies, categories automatically. I don't want to add if condition for every main_get_tab function.

How can I get them and retrieve posts by langages.

Thanks,

January 28, 2014 at 4:15 pm #329159

Andrey
Supporter

Languages: English (English ) Russian (Русский )

Timezone: Europe/Kiev (GMT+02:00)

Dear jin woo,

I need research your class/functions and will update you here as soon as I know more .

Thanks,
Andrey

January 29, 2014 at 9:17 pm #330113

Andrey
Supporter

Languages: English (English ) Russian (Русский )

Timezone: Europe/Kiev (GMT+02:00)

Dear jin woo,

I want you to know we have not forgotten about you.

Thanks,
Andrey

January 30, 2014 at 12:15 pm #330472

sarah.n
Supporter

Languages: English (English ) German (Deutsch )

Hello jin woo,

This is Sarah here. Andrey asked me to assist so this is why I am replying here 🙂

If you want to built a custom query using taxonomy parameters: http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters you will find it easier to work with the term id rather than then slug.

Since you know the slug and you can find the term id from the slug using get_term_by()

Here is a very basic example which you can use as a starting point to incorporate the login in your code:

/* WPML
 * Built custom query using taxonomy parameters
 */
// get the term object from slug
$term_obj = get_term_by( 'slug', 'uncategorized', 'category' );

//query args
$term_slug_args = array(
	'post_type' => 'post',
	'tax_query' => array(
		array(
			'taxonomy' => 'category',
			'field' => 'id',
			'terms' => icl_object_id($term_obj->term_id, 'category', TRUE)
		)
	)
);

//build query
$term_slug_query = new WP_Query( $term_slug_args );

//loop
while ( $term_slug_query->have_posts() ) : $term_slug_query->the_post();
	//modify the output code to fit your needs
	?>
	<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a><br/>
	<?php
endwhile;
wp_reset_query();

Hope this helps.

January 30, 2014 at 12:34 pm #330490

Andrey
Supporter

Languages: English (English ) Russian (Русский )

Timezone: Europe/Kiev (GMT+02:00)

Dear jin woo,

Please try Sarah's suggestion.

Thanks,
Andrey

February 3, 2014 at 2:34 am #331901

jin woo

Sahar, thanks, it works like a charm.

before I close as resolved, I need to confirm one thing.

1st, we changed taxonomy-id instead of slug and we used your codes. but it didn't work.

            'terms' => icl_object_id($term_obj->term_taxonomy_id, 'category', TRUE)

I tried "term_id" instead of "term_taxonomy_id". then it works. is it ok?
I know term_taxomomy_id is a unique id for term and term_taxonomy.

Anyway it works great and I just wonder if I am doing right.

If you have time, please let me know. I will close it soon anyway.

Thanks Andrey and Sahar!

February 3, 2014 at 7:53 am #331946

sarah.n
Supporter

Languages: English (English ) German (Deutsch )

Oh yes, of course! It should be:

'terms' => icl_object_id($term_obj->term_id, 'category', TRUE)

Thanks for catching that. I will correct the code in my previous comment just in case someone else may see it and decide to use it 🙂

February 3, 2014 at 9:53 am #332051

Andrey
Supporter

Languages: English (English ) Russian (Русский )

Timezone: Europe/Kiev (GMT+02:00)

Dear jin woo,

Glad to hear that it now works.

Thanks,
Andrey

February 4, 2014 at 2:04 am #332651

jin woo

Thanks Sarah and Andrey.

You guys are the best!!