Skip Navigation

[Resolved] Sort view by multiple custom fields

This support ticket is created 2 years, 3 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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9: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/Karachi (GMT+05:00)

This topic contains 13 replies, has 2 voices.

Last updated by Waqar 2 years, 3 months ago.

Assisted by: Waqar.

Author
Posts
#2445507

I want to sort the view with multiple custom fields

Is there any documentation that you are following?
https://toolset.com/forums/topic/how-to-apply-custom-code-to-order-view-by-multiple-custom-fields/

i have tried to customize it for my scenario but it shows no effect.
this is how far I have come:
i have copied the following code into the fuctions.php of my theme with the id of the desired view and the custom fields
///
function tssupp_modify_orderby( $view_args, $view_settings, $view_id ){

if ( 5325 == $view_id ) { // Edit ID

$view_args['meta_query'] = array(

'relation' => 'AND',

'hersteller' => array(
'key' => 'wpcf-hersteller',
'compare' => 'EXISTS',
),
'weinart' => array(
'key' => 'wpcf-weinart',
'compare' => 'EXISTS',
),
'produktname' => array(
'key' => 'wpcf-produktname',
'compare' => 'EXISTS',
),
'jahrgang' => array(
'key' => 'wpcf-jahrgang',
'compare' => 'EXISTS',
),
);

$view_args['orderby'] = array(
'hersteller' => 'ASC',
'weinart' => 'ASC',
'produktname' => 'ASC',
'jahrgang' => 'ASC'
);
}

return $view_args;
}
add_filter( 'wpv_filter_query', 'tssupp_modify_orderby', 101, 3);
//

What is the link to your site?
cantinadelmulino.website/weine

#2446891

Hi,

Thank you for contacting us and I'd be happy to assist.

To troubleshoot this on my test website, I'll need to see which types of custom fields are you trying to use in this function.

Can you please share temporary admin login details, in reply to this message?

Note: Your next reply will be private and though no changes will be made on your website, making a complete backup copy is recommended before sharing the access details.

regards,
Waqar

#2447515

Thank you for sharing the admin access details.

I couldn't find the page where the view "Weine View Search" was in use, so I added a new page with this view named "Test page from Toolset support":
{yourwebsite.com}/test-page-from-toolset-support/

For better visibility of the values, I've changed the loop content of the view from:


<ul class="wpv-loop js-wpv-loop product-listing list list-view-change negativ-color">
		<wpv-loop>
          <li><a href="[wpv-post-url]">
              <div class="listing-img wein">
              <img onerror="this.onerror=null;this.src='<em><u>hidden link</u></em>';" 
 src="<em><u>hidden link</u></em> field='produktbild'][/types]-490x700.jpg">
              </div>
            <div class="info">
              <div class="inner-info">
                <span class="subskription-term" style="position: absolute; top:2px; font-size: 11px;">[wpv-post-taxonomy type="subskription" format="name"]</span>
                <b>[types field='produktname'][/types]</b>
              [wpv-conditional if="( $(wpcf-jahrgang) ne '' )"] <div class="jahrgang"><span class="separator">|</span> [types field='jahrgang'][/types]</div>[/wpv-conditional]
              [wpv-conditional if="( $(wpcf-hersteller) ne '' )"]<div class="hersteller"><span class="separator">|</span>[types field='hersteller'][/types]</div>[/wpv-conditional]
             	[wpv-conditional if="( $(wpcf-weinbau) ne '' )"]<div class="weinanbau"><span class="separator">|</span>[types field='weinbau'][/types]</div>[/wpv-conditional]
                 
              <div class="land-region">
             	<span class="separator">|</span>[types field='region-product'][/types], [types field='land-product'][/types]
              </div>
                [wpv-conditional if="( $(wpcf-weinart) ne '' )"]<div class="weintyp-symbol"><span class="separator">|</span>[types field='weinart'][/types]</div>[/wpv-conditional]
              </div>
            	
              
              <div class="preis">
                [wpv-conditional if="( $(wpcf-grosse) ne '' )"]<span class="grosse">[types field='grosse'][/types]</span>[/wpv-conditional]
                [wpv-conditional if="( $(wpcf-sale-price) eq '' )"]
               <b>[format_money price="[types field='product-price' output='raw'][/types]"]</b>
                [/wpv-conditional]
                [wpv-conditional if="( $(wpcf-sale-price) ne '' )"]
                <span class="grosse" style="position: relative; top:-15px; left:45px; margin-left:-45px;"><s>[format_money price="[types field='product-price' output='raw'][/types]"]</s></span>
                <b>[format_money price="[types field='sale-price'][/types]"]</b>
				[/wpv-conditional]
              </div>
            </div>
            </a></li>
		</wpv-loop>
	</ul>

To:


<ul class="wpv-loop js-wpv-loop product-listing list-view-change negativ-color">
		<wpv-loop>
          <li>
            <strong>[wpv-post-title] ([wpv-post-id])</strong><br>
            <div class="grid-container-spc">
            hersteller: [wpv-post-field name="wpcf-hersteller"]
            </div>
            <div class="grid-container-spc">
            weinart: [wpv-post-field name="wpcf-weinart"]
            </div>
            <div class="grid-container-spc">
            produktname: [wpv-post-field name="wpcf-produktname"]
            </div>
            <div class="grid-container-spc">
            jahrgang: [wpv-post-field name="wpcf-jahrgang"]
            </div>
            <hr>
          </li>
		</wpv-loop>
	</ul>

If you'll check the results on the newly created test page, the custom code seems to be setting the order of the results, as expected.

Important note: As the custom code is using the 'EXISTS' operator, it will not order the results based on the values stored in those fields. It will only check whether a custom field record for the post exists or not, without evaluating the saved values.

#2447543

Thank you
The URL to the view is: /test-page-from-toolset-support/weine/

Except that my layout no longer shows the correct fields, I don't see any difference in the ordering.

I think my code in the php functions is not correct.
"As the custom code is using the 'EXISTS' operator, it will not order the results based on the values stored in those fields."
How can I change this so that it is ordered based on the values stored in these fields?"

#2447593
now-false.png
Correct.png

Sorry the url is: {mywebsite.com}/weine

it is not easy to see if the order is correct, this may helps
If you enter "Schott" in the search, all "rotwein" [weinart] should come first, these should be sorted by [produktname] and then by [jahrgang]
In the attachet images you should see how it should be sorted

Thank you!

#2448031

Thanks for writing back and I stand corrected about my comments about the use of the 'EXISTS' operator.

During testing on my website, that custom code worked correctly so it seems something specific to your website is involved.

Do I have your permission to download a clone/snapshot of your website, to investigate this deeper on a different server?

#2448043

Yes of course: You have your permission to download a clone/snapshot of your website, to investigate this deeper on a different server

Thanks a lot!

#2449195

Thank you for the permission. I've downloaded your website's clone.

I'm currently performing some testing on this clone and will share the findings, as soon as this testing completes.

Thank you for your patience.

#2451079

Thank you for waiting.

I made some adjustments in the custom code for the view's ordering and it seems to be working
( screenshot: hidden link )


function tssupp_modify_orderby( $view_args, $view_settings, $view_id ){
if ( 5325 == $view_id ) { // Edit ID
  $view_args['meta_query'] = array(
    'relation' => 'AND',
    'hersteller' => array(
      'key' => 'wpcf-hersteller',
      'compare' => 'EXISTS',
    ),
    'weinart' => array(
      'key' => 'wpcf-weinart',
      'compare' => 'EXISTS',
    ),
    'produktname' => array(
      'key' => 'wpcf-produktname',
      'compare' => 'EXISTS',
    ),
    'jahrgang' => array(
      'key' => 'wpcf-jahrgang',
      'compare' => 'EXISTS',
    ),
  );

  $view_args['orderby'] = array(
    'hersteller' => 'ASC',
    'weinart' => 'ASC',
    'produktname' => 'ASC',
    'jahrgang' => 'ASC'
  );

  unset($view_args['wpv_orderby']);
  unset($view_args['wpv_order']);
}

  return $view_args;
}
add_filter( 'wpv_filter_query', 'tssupp_modify_orderby', 101, 3);

An important point to note here is that since the ordering is set to use all 4 fields, the WordPress query considers the values in all of 4 fields. It will not work to sort the custom field values one-by-one or one column at a time. This is why all the records with 'Rotwein' values in the "weinart" column are not showing first, as the values in the 'produktname' and 'jahrgang' columns also matter.

#2452773

hello Waqar
Thanks for the effort.

The posts are sorted differently, but not as desired. It is only sorted by Post Title.
No matter if I change the order of "$view_args['orderby'] = array" or take away the whole code, it is always sorted by Post Title.

And yes I don't want to sort only by color or only by title. I want it to be sorted by manufacturer and color and name and year.
I will give you an example:

It's a wine shop. it wants to show that producer A has these red wines and these white wines. So step 1 is to sort all producers. Step two is to distinguish between the colors. In step three, we look at the red wines of a producer. They should be sorted by name and vintage. Because there are products with the same name, but a different vintage.
Do you see how we want to sort it?

It would be a huge upgrade if we could manage to sort by producer and color. So step 1 and two.

Can you please check again?

#2453613

Thank you for sharing these details.

For this specific requirement, the conventional WordPress query ordering won't work.

I have a couple of workaround ideas that most likely will involve some script-based ordering, but, I'll have to test them first.

If you're fine with the script-based ordering, I can do some testing over the weekend and share the suggestions with you accordingly.

#2453659

Yes scripting solutions would of course also be ok.

Thank you very much!

#2455667

Just wanted to let you know that I'm still working on this.

I haven't been able to make it work yet, but, I'll keep updating you on the progress.

#2456945

Thank you for waiting.

I tried a number of custom script methods, but, unfortunately, couldn't sort the results, the way you plan to show them.

Thinking this through, your requirement doesn't only include sorting/ordering of the results, but it also needs to have the grouping ( by the producer and the wine type).

Considering this requirement and the performance aspect in mind, I'll recommend switching from custom fields to custom taxonomies for the 'hersteller' and 'weinart' data.

This way you'll be able to show the results grouped by these taxonomies:

1. Your first main view will be set to show the taxonomy items from "hersteller", to cycle through all the manufacturers.
2. A second view will be nested inside the main view to show the taxonomy items from 'weinart', to cycle through the current manufacturer's wine types/colors.
3. A third view will be nested inside the second view to show the product post items with a query filter to limit the results from the current manufacturer and the wine type. And you can set the order of this view to order by 'produktname' and 'jahrgang' custom fields.

Important note: This structure will help you in displaying the product results in a certain way, but you won't be able to use the view's front-end search query filters or the pagination feature, as there will be multiple views involved.