Skip Navigation

[Gelöst] Display only the latest post with duplicated taxonomy

Dieser Thread wurde gelöst. Hier ist eine Beschreibung des Problems und der Lösung.

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 vor 6 Jahre, 2 Monate. 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 Antworten, has 2 Stimmen.

Last updated by kaneB vor 6 Jahre, 1 Monat.

Assisted by: Minesh.

Author
Artikel
#611611

I have a CPT that list the "prices" and the "stores" that sells them. Each "price" has a field for "Date" and "Store" is set as a taxonomy of "price". There can be multiple "prices" added and some of they would have the same "store".

EG:
Price 1 Store 1 Date 31Jan
Price 2 Store 2 Date 1Feb
Price 3 Store 1 Date 1Feb

My objective is to display only one "price" with the latest "date" for a particular "store".

Price 2 Store 2 Date 1Feb
Price 3 Store 1 Date 1Feb

How should i achieve this with views?

#611693

Minesh
Supporter

Languages: Englisch (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - as per the information shared by you about your structure, "Store" is setup as taxonomy - correct? If yes:

You should create nested view - one view to loop through your store taxonomy, so you should create a new taxonomy view and another view to loop through your posttype and add this view within the taxonomy view's loop output.

Please check following similar article that may help you:
=> https://toolset.com/2015/07/how-to-group-posts-by-category/

Related support ticket:
=> https://toolset.com/forums/topic/group-results-by-category/#post-408256

#612649

How would this nested views solve me "showing only the post with the latest date"? I understand it clubs the taxonomy together but I need to display only one - the one with the latest date.

Thanks.

#612661

Minesh
Supporter

Languages: Englisch (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - what if you set your view to order by date desc from your view's order section.

#617275

I have tried this and my issue now is that while i can club the "stores" together, i cannot sort the price because sorting will follow the taxonomy view's sorting. How do i achieve that?

Price 1.00 Store 1 Date 31Jan
Price 2.00 Store 2 Date 1Feb
Price 3.00 Store 1 Date 1Feb

It now displays:
Price 3.00 Store 1 Date 1Feb
Price 2.00 Store 2 Date 1Feb

I need it to display :
Price 2.00 Store 2 Date 1Feb
Price 3.00 Store 1 Date 1Feb

In addition, the "Price" is a child post of "Product" post type and the suggested taxo nested views currently sits in a child post view of the product content template.

Product (single page content template)
> Price View (child post)
>>Taxo view (limit 1)
>>>Price view

Thanks.

#617290

Minesh
Supporter

Languages: Englisch (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - this needs custom query as you can not control the child view order by and that you can not merge with order by of parent view.

Still - it will be great if you can share temporary access details with problem URL and what view's you are using and I will check if I can offer you any solution.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#617736

Minesh
Supporter

Languages: Englisch (English )

Timezone: Asia/Kolkata (GMT+05:30)

Ok - thanks for sharing all required access and I checked your current setup.

You said:
The display in view 3.1 is the correct prices but i want it to display only the latest price per "store".
==> This means that the you want to display latest price per store with latest date with date field belongs to "Price Date" - correct?

So, for example:
=> hidden link
-- When displaying above project - it should display the price which is having date value 07/02/2018 as "Price Date" - correct?

#618110

Yes Dinesh, your understanding is correct. In this particular product, the price that should be displayed is:
2.87 07/02/2018
3.88 06/02/2018

as they belong to different "store".

#618292

Minesh
Supporter

Languages: Englisch (English )

Timezone: Asia/Kolkata (GMT+05:30)

Perfect. I need FTP access details as I need to add some filters.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I would additionally need your permission to de- and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important. If you agree to this, please use the form fields I have enabled below to provide temporary access details (wp-admin and FTP).

I have set the next reply to private which means only you and I have access to it.

#619068

Minesh
Supporter

Languages: Englisch (English )

Timezone: Asia/Kolkata (GMT+05:30)

Could you please check now.

I've added following filter to your current theme's functions.php file:

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="current-price" id="'.$v.'"][/types]');
		//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;
}

And I can see now it's displaying the correct results - could you please confirm.
=> hidden link

#619564

Thanks Dinesh, this is definitely one big leap towards the desired outcome.

I have tested, and seem to have some small issue: I have added some new prices both into new "store" and existing "store", and it seems it wont take the latest "price".

hidden link
Price 1659 (2.18) not showing (should override price 1656-2.19)
Price 1662 (2.86) not showing (should override price 1630-2.87)
Price 1665 (3.87) not showing (should override price 1626-3.88)

I also only want this checking logic to apply to "normal" price only. "Normal" is in the price type post field.

Thanks!

#619569

I have looked at the code again, i think you made comparison to "price" instead of "price date". My test confirms that the filter above compares the unique "price" instead of the date. See price 1670 (2.19) manage to replace price 1656 (2.19).

#619597

Minesh
Supporter

Languages: Englisch (English )

Timezone: Asia/Kolkata (GMT+05:30)

I've changed the date field with the code I shared before:

	$p = do_shortcode('[types field="price-date" id="'.$v.'"][/types]');

Could you please confirm now that it compares that price date field now.

#619879

Thanks Dinesh, seems to be comparing the date field now with your amendment.

Two queries:
1. It doesn't seem to work backwards, as in if i changed an old date to a later date, it will still show the "latest added date". Is it because the variable was not reset?

2. How do i compare this only on "normal" price type? It's one of the post field.

Great thanks.

#619883

Minesh
Supporter

Languages: Englisch (English )

Timezone: Asia/Kolkata (GMT+05:30)

Sorry to correct you but my name is Minesh 🙂

Two queries:
1. It doesn't seem to work backwards, as in if i changed an old date to a later date, it will still show the "latest added date". Is it because the variable was not reset?
==> Could you please tell me example record which you edited and should display with the result? or what exact result it should show after changing the date.

2. How do i compare this only on "normal" price type? It's one of the post field.
==> What is your normal price type field? how you want to compare it? could you please open a new ticket for your each new question. This will help other users searching on the forum.

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