Skip Navigation

[Resolved] How do I display a search result first on Col A then Col B?

This support ticket is created 4 years, 1 month 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 13 replies, has 4 voices.

Last updated by Nigel 4 years ago.

Assisted by: Nigel.

Author
Posts
#1826319
Screenshot from 2020-10-27 12-31-25.png

Tell us what you are trying to do?

Trying to sort search results first by thickness, then by width. For instance, an output ordered like this:

¼” x 1”
¼” x 1-1/2”
¼” x 1-3/4”

... where the thickness is first, and the width is second. I have the search sorted by thickness, but the secondary sort for the page doesn't let me access my custom sorts. The client prefers to not display a "sort by" dropdown. And one final note, while I output the american measurement in inches (1/4 or 1), I have them sorted by a number field with decimals. I call them Thickness D and Width D.

What is the link to your site?

hidden link

Thank you for your help!

#1826855

Hello,

The "Secondary Sorting" option of Toolset View block supports only WP built-in post fields, you might consider custom codes, for example, use filter hook wpv_filter_query to trigger a PHP function:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query
In this PHP function, order the result manually, see WP document:
https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters
There is an example for "‘orderby’ with multiple ‘meta_key’s"

#1833691

Thank you for that. I can work with that code.

Where/how does this code get added into the form on WordPress? Do I edit the page with the search? If so, I’m not seeing how to insert the code.

#1833947

You can try Toolset Custom Code, see our document:
https://toolset.com/documentation/adding-custom-code/

#1836837

I've gotten used to the Custom Code tab, but I can't seem to get the results I need. Can you assist? I'm not sure I'm using the correct information for the key; using the slug for now. Not sure about the "0" placeholder in add-filter. I think I have the form code correct with 59437. Thank you!


add_filter( 'wpv_filter_query', 'q', 0, 59437 );

$q = new WP_Query( array(
    'meta_query' => array(
        'relation' => 'AND',
        'thickD_clause' => array(
            'key' => 'mouldings-thickness-d',
            'compare' => 'EXISTS',
        ),
        'widthD_clause' => array(
            'key' => 'mouldings-width-d',
            'compare' => 'EXISTS',
        ), 
    ),
    'orderby' => array( 
        'thickD_clause' => 'ASC',
        'widthD_clause' => 'ASC',
    ),
) );

ERRORS

Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'q' not found or invalid function name in /homepages/42/d272698695/htdocs/HH2020/wp-includes/class-wp-hook.php on line 287

Warning: in_array() expects parameter 2 to be array, null given in /homepages/42/d272698695/htdocs/HH2020/wp-content/plugins/toolset-blocks/embedded/inc/filters/wpv-filter-status-embedded.php on line 45

Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'q' not found or invalid function name in /homepages/42/d272698695/htdocs/HH2020/wp-includes/class-wp-hook.php on line 287

Warning: in_array() expects parameter 2 to be array, null given in /homepages/42/d272698695/htdocs/HH2020/wp-content/plugins/toolset-blocks/embedded/inc/filters/wpv-filter-status-embedded.php on line 45

Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'q' not found or invalid function name in /homepages/42/d272698695/htdocs/HH2020/wp-includes/class-wp-hook.php on line 287

Warning: in_array() expects parameter 2 to be array, null given in /homepages/42/d272698695/htdocs/HH2020/wp-content/plugins/toolset-blocks/embedded/inc/filters/wpv-filter-status-embedded.php on line 45

#1838353

You need to follow the document I mentioned above to setup the custom PHP codes:
https://toolset.com/documentation/programmer-reference/views-filters/#wpv_filter_query

click "Usage examples", there is an example for how to use filter hook "wpv_filter_query", and follow WP document to setup the filter parameters:
https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters

If you still need assistance for it, please provide a test site wit the same problem, also point out the problem page URL and view URL, I can setup a demo for you.

#1840073

Just bumping this.

#1841373

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Barney

Sorry for the delay in your receiving a response, Luo is on vacation and we have a couple of unexpected absences that is leaving us a bit stretched providing cover.

I'm looking into this now.

This would be reasonably straightforward to code (for a coder 🙂 ) if you didn't also have a filter for a custom field in this View which may or may not be active for any given update, and that makes it slightly more complex.

I can do that for you—I have a similar scenario set up on my own test site—but I'm reaching the end of my day, so I may not have it for you until the (my) morning.

Thanks for your patience.

#1841465

Hello Nigel,

I wondered what happened to Luo. Thanks for getting back to me. I couldn't wait for Luo's response so I reached out to Toolset contractors.

Here is the solution to this issue.

add_filter('wpv_filter_query', 'thickd_widthd_asc', 199, 3);
function thickd_widthd_asc($query_args, $view_settings, $view_id ) {
  $view_ids = array( 64433 );
  if( in_array( $view_id, $view_ids)) {
    $query_args['meta_query'] = array(
        'relation' => 'AND',
        'year_clause' => array(
            'key' => 'wpcf-mouldings-thickness-d',
            'compare' => 'EXISTS',
            //'type' => 'NUMERIC',
        ),
        'month_clause' => array(
            'key' => 'wpcf-mouldings-width-d',
            'compare' => 'EXISTS',
            //'type' => 'NUMERIC',
        ),
    );
    $query_args['orderby'] = array(
        'year_clause' => 'ASC',
        'month_clause' => 'ASC',
    );
  }
  return $query_args;
}

Here is the thread that my contractor used to fix my issue https://toolset.com/forums/topic/sort-by-2-custom-fields/

#1841467

My issue is resolved now. Thank you!

#1841791

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Barney

Sorry you had to reach out to someone else for a solution.

That code, that's the easy part, inasmuch as it simply imposes the meta query for the width and thickness (they didn't think to rename the year and month clauses?), but it doesn't allow for any other custom field filter. And I believe your page has a custom field filter does it not? I don't think that will work any more if you have the above code, but if you are not experiencing any problems, then great...

#1842227

I spoke too soon.

While the sort works, it has broken the drop-down sort by category and instead returns a result of 100% of items, instead of just a category like Baseboards or Siding.

Can you suggest a fix?

#1842355
#1843423

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

I did say it was trickier than the simple code example above, which doesn't take account of other custom field filters being applied.

Replace that code with the following, it should produce the correct ordering, as well as support additional filters.

add_filter('wpv_filter_query', 'ts_custom_sort', 101, 3);
function ts_custom_sort($query_args, $view_settings, $view_id)
{

    $view_ids = array(64433);

    if (in_array($view_id, $view_ids)) {

        if ( !isset( $query_args['meta_query'] ) ){

            $query_args['meta_query'] = array(
                'relation'  => 'AND'
            );            
        }

        $query_args['meta_query']['thickness_clause'] = array(
                'key' => 'wpcf-mouldings-thickness-d',
                'compare' => 'EXISTS',
                'type' =>  'NUMERIC'
        );

        $query_args['meta_query']['width_clause'] = array(
                'key' => 'wpcf-mouldings-width-d',
                'compare' => 'EXISTS',
                'type' =>  'NUMERIC'
        );

        $query_args['orderby'] = array(
            'thickness_clause' => 'ASC',
            'width_clause' => 'ASC',
        );

        unset($query_args['meta_key']);

    }

    return $query_args;
}