Skip Navigation

[Resolved] Display only the latest post with duplicated taxonomy

This thread is resolved. Here is a description of the problem and solution.

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 6 years, 2 months ago. 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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)

This topic contains 22 replies, has 2 voices.

Last updated by kaneB 6 years, 1 month ago.

Assisted by: Minesh.

Author
Posts
#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

Languages: English (English )

Timezone: 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

Languages: English (English )

Timezone: 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

Languages: English (English )

Timezone: 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

Languages: English (English )

Timezone: 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:
=> hidden link

#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!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.