Skip Navigation

[Résolu] Display only the latest post with duplicated taxonomy

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem:
How to Order child posts by taxonomy with custom date field

Solution:
You can use view's filter hook wpv_filter_query to modify the view's query on fly and adjust it as per your requirement by hooking custom query filters to it.

You can find the proposed solution, in this case, with the following reply:
=> https://toolset.com/forums/topic/display-only-the-latest-post-with-duplicated-taxonomy/page/2/#post-620541

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

This support ticket is created Il y a 6 années et 10 mois. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

Ce sujet contient 22 réponses, a 2 voix.

Dernière mise à jour par kaneB Il y a 6 années et 9 mois.

Assisté par: Minesh.

Auteur
Publications
#619891

Sorry about that Minesh!

Price 1665: the date has been changed to 1/3, so it should be displayed instead of the Price 1673 that was dated 27/2. Note that 1665 is actually created before 1673, but i edited the date to a later date.

On the "apply the checking on normal price type", its related to this code. I have a post field price-type that records "normal" or "offer". So i only want the algorithm to work on price-type = "normal", such that if it is an offer price, it will display all; if it is "normal" price, it will display only the latest price for each store (already what the code achieves now).

Thanks!

#619946

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

I've adjusted the code as given under:

add_filter( 'wpv_filter_query', 'prefix_show_only_current_author',99,3 );
function prefix_show_only_current_author( $query,$view_settings, $view_id) {
   
   if ( $view_id == 1534 ) { // if
		//echo "<pre>";
		//print_r($query);
		
		$x = $query['post__in'];
		$unique = array();
		$tmp_p = '';
		$tmp_array = array();
		$post_ids = array();
		
		foreach($x as $k=>$v):
		$tax = do_shortcode('[wpv-post-taxonomy type="store" format="name" id="'.$v.'"]');
		$p = do_shortcode('[types field="price-date" output="raw" id="'.$v.'"][/types]');
		$price_type = do_shortcode('[types field="price-type" output="raw" id="'.$v.'"][/types]');
		
		if($price_type=='normal'){
		//echo "<br/>tmp==".$tmp_p;
		
			if(!array_key_exists($tax,$tmp_array)){
				$unique[$tax]['post_id'] = $v;
				$unique[$tax]['tax'] = $tax;
				$unique[$tax]['price'] = $p;
				$tmp_array[$tax] = $tax;
			}else{
				
				if($p > $unique[$tax]['price']){
					$unique[$tax]['post_id'] = $v;
					$unique[$tax]['price'] = $p;
				} 
			}
		}
		endforeach;
		
		foreach($unique as $k=>$v):
			$post_ids[] = $v['post_id'];
		endforeach;
		
		
		$query['post__in'] = $post_ids;
		
		
   }
	
    return $query;
}

Is this expected output now? 🙂

#620437

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Is the solution I shared with you help you to resolve your issue? - Could you please confirm.

#620528

Thanks Minesh, the logic for "normal" price works fine now even if i edited the date.

But the "not normal" price type is not appearing at all. I think we are missing an "if price type <> normal"?

Thanks a lot.

#620529

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Do you mean that if price type is not normal you still want to display such post without applying the logic we set to compare the date?

#620535

That's correct Minesh, it should display as is.

#620541

Minesh
Supporter

Les langues: Anglais (English )

Fuseau horaire: Asia/Kolkata (GMT+05:30)

Could you please check now. I've adjusted the code as given under:

add_filter( 'wpv_filter_query', 'prefix_show_only_current_author',99,3 );
function prefix_show_only_current_author( $query,$view_settings, $view_id) {
   
   if ( $view_id == 1534 ) { // if
		//echo "<pre>";
		//print_r($query);
		
		$x = $query['post__in'];
		$unique = $tmp_array =  $post_ids = array();
		$not_normal_ids = array();
		
		foreach($x as $k=>$v):
		$tax = do_shortcode('[wpv-post-taxonomy type="store" format="name" id="'.$v.'"]');
		$p = do_shortcode('[types field="price-date" output="raw" id="'.$v.'"][/types]');
		$price_type = do_shortcode('[types field="price-type" output="raw" id="'.$v.'"][/types]');
		
		if($price_type=='normal'){
		
			if(!array_key_exists($tax,$tmp_array)){
				$unique[$tax]['post_id'] = $v;
				$unique[$tax]['tax'] = $tax;
				$unique[$tax]['price'] = $p;
				$tmp_array[$tax] = $tax;
			}else{
				
				if($p > $unique[$tax]['price']){
					$unique[$tax]['post_id'] = $v;
					$unique[$tax]['price'] = $p;
				} 
			}
		}else{
			$not_normal_ids[] = $v;
		}
		endforeach;
		
			
		foreach($unique as $k=>$v):
			$post_ids[] = $v['post_id'];
		endforeach;
		
		
		$query['post__in'] = array_merge($post_ids,$not_normal_ids);
		
		
   }
	
    return $query;
}

And it's displaying all posts now:
=> lien caché

#620791

Hi Minesh, i think this is working perfect now. I will replicate and further test on this.

Great thanks for your patience and help!