Custom sorting fields doesn’t seem to work properly

  • pridigital
    Participant
    8 months, 3 weeks ago #35284

    Hi there,
    I’m facing two issues with the sorting functionality. Here is the scenario explained.

    In the admin area:

    • I have a custom post type (CTP_1)
    • I have a “text, sortable” ACF field (ACF_1) configured in the “2.2 Data” tab
    • I’ve added ACF_1 to the “2.5 Sorts” tab in WP with the text “My field” for the translation.

    In my code:

    • I’m using the WPSOLR_Events::WPSOLR_FILTER_SORT_FIELDS filter to override the sort fields for CPT_1

    `
    add_filter( WPSOLR_Events::WPSOLR_FILTER_SORT_FIELDS, array( ‘x’ );
    public function x( $default_sort_fields ) {
    return get_post_type() === ‘ACF_1’ ? array(‘acf_1_str_asc’) : $default_sort_fields;
    }
    `

    In the frontend:

    • I’m rendering the sort widget

    `
    the_widget( ‘wpsolr\core\classes\ui\widget\WPSOLR_Widget_Sort’ );
    `

    All of the above seems to work but… not really.

    1. The sort widget doesn’t render the translation (“My field”). Instead it shows the field key “ACF_1”
    2. The sort URL parameter doesn’t get set when a sort option is selected in the widget. It fires a 301 redirects instead.

    One more time I appreciate your support with this request! 🙂

    wpsolr
    Keymaster
    8 months, 3 weeks ago #35285

    I’m using the WPSOLR_Events::WPSOLR_FILTER_SORT_FIELDS filter to override the sort fields for CPT_1

    You do not need that.

    I’ve added ACF_1 to the “2.5 Sorts” tab in WP with the text “My field” for the translation.

    This should be enough to see your field in the sort list, with its custom label.

    pridigital
    Participant
    8 months, 3 weeks ago #35286

    I’m using the WPSOLR_Events::WPSOLR_FILTER_SORT_FIELDS filter because I want to show different sort options for different post types. However, by doing that the HTML select returns the key for the label instead of the translation.

    • This reply was modified 8 months, 3 weeks ago by pridigital. Reason: more details
    wpsolr
    Keymaster
    8 months, 3 weeks ago #35290

    Is ‘acf_1_str_asc’ in $default_sort_fields ?

    pridigital
    Participant
    8 months, 3 weeks ago #35296

    Nope. Only after I manually add it in the code. Which is the weird part because I’ve added it in the backend through the WPSolr interface.

    wpsolr
    Keymaster
    8 months, 3 weeks ago #35297

    Please show here your sort settings screen.

    pridigital
    Participant
    8 months, 3 weeks ago #35298

    As you can see here the item number would be my “acf_1” field.

    Also for reference this is what I get from the default sort fields variable before overwriting it.

    array(2) { [0]=> string(17) "sort_by_date_desc" [1]=> string(16) "sort_by_date_asc" }

    wpsolr
    Keymaster
    8 months, 3 weeks ago #35301

    item_number must be an active sort in your sort screen, so you can see it in your sort widget.

    Then your hook can remove it from the default sort fields wherever you do not need it.

    pridigital
    Participant
    8 months, 3 weeks ago #35308

    Yes, but item_number is already an active sort in my sort screen. You can see it in the screenshot I shared before. Am I missing something?

    wpsolr
    Keymaster
    8 months, 3 weeks ago #35309

    item_number should appear in $default_sort_fields.

    Do you use several views?

    pridigital
    Participant
    8 months, 3 weeks ago #35311

    No, I don’t. I’m using the “Default View”.

    wpsolr
    Keymaster
    8 months, 3 weeks ago #35320

    Remove your code. Do you see item_number in the WPSOLR sort widget?

    pridigital
    Participant
    8 months, 3 weeks ago #35362

    Yes, if I remove my code I see the it. However, it also shows other items that are not supposed to be shown.

    Let’s say I have the following scenario:

    Tax 1
    – Item Number
    – Date (should be hidden)
    Tax 2
    – Price
    – Date

    If I remove my code then both taxonomies will show the date sort.

    wpsolr
    Keymaster
    8 months, 3 weeks ago #35365

    I thought you wanted to control visibility of item_number, not date ?

    pridigital
    Participant
    8 months, 2 weeks ago #35376

    Hi there 🙂 the field name is irrelevant. Those were just examples, but yeah. Let’s say then:

    Tax 1
    – Item Number (should be hidden)
    – Date
    Tax 2
    – Item Number (should NOT be hidden)
    – Price
    – Date

Viewing 15 posts - 1 through 15 (of 18 total)

You must be logged in to reply to this topic.