• Resolved John

    (@artoftech)


    Hi,
    For the courses archive page, as a follow-on to adding sort capability, I now have an issue with the sort default, which is set to post_date. Not only do I not need this option, but I need to change the default to post_title (ASC). I can get rid of the option from the sort drop-down, but that has no effect on it as it just continues to sort this way labelled incorrectly.

    Here is the code that I think needs to be overridden:

    public function html_order_by( string $default_value = 'post_date' ): string {
    $html_wrapper = [
    '<div class="courses-order-by-wrapper">' => '</div>',
    ];

    $values = apply_filters(
    'learn-press/courses/order-by/values',
    [
    'post_date' => esc_html__( 'Newly published', 'learnpress' ),
    'post_title' => esc_html__( 'Title a-z', 'learnpress' ),
    'post_title_desc' => esc_html__( 'Title z-a', 'learnpress' ),
    'price' => esc_html__( 'Price high to low', 'learnpress' ),
    'price_low' => esc_html__( 'Price low to high', 'learnpress' ),
    'popular' => esc_html__( 'Popular', 'learnpress' ),
    ]
    );

    $content = '<select name="order_by" class="courses-order-by">';
    foreach ( $values as $k => $v ) {
    $content .= '<option value="' . $k . '" ' . selected( $default_value, $k, false ) . '>' . $v . '</option>';
    }
    $content .= '</select>';
    //$content .= $default_value;

    return Template::instance()->nest_elements( $html_wrapper, $content );
    }

    located in this file: /wp-content/plugins/learnpress/inc/TemplateHooks/Course/ListCoursesTemplate.php. Even changing it here to post_title without the proper technique for child theming has no effect:

    public function html_order_by( string $default_value = 'post_title' ): string {

    Can you shed some light on the proper technique for changing this default?

    Thanks as always,
    John

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support brianvu-tp

    (@briantp)

    Hi John,

    Thank you for reaching out to us!

    Based on your description, we understand that you want to change the default sort order on the courses archive page from post_date to post_title. If that is correct, you can try adding the following code to the functions.php file of your child theme:

    // Modify the default order_by to post_title
    add_action('learn-press/courses/handle_params_for_query_courses', function($filter, $param) {
        if (empty($param['order_by'])) {
            $filter->order_by = 'post_title';
            $filter->order = 'ASC';
        }
    }, 10, 2);
    
    // Modify the default order_by in section/top hook
    add_filter('learn-press/layout/list-courses/section/top', function($section_top, $courses, $settings) {
        if (!isset($settings['order_by'])) {
            $settings['order_by'] = 'post_title';
        }
    
        $listCoursesTemplate = LearnPress\TemplateHooks\Course\ListCoursesTemplate::instance();
    
        $section_top['order_by'] = $listCoursesTemplate->html_order_by($settings['order_by']);
    
        return $section_top;
    }, 10, 3);

    Please add the code and test it on your site to see if it works as expected. If you encounter any issues or have further questions, feel free to reach out to us again.

    Best regards,
    Brianvu-tp

    Thread Starter John

    (@artoftech)

    Thanks Brianvu-tp,
    Works a charm!
    Best,
    John

    Plugin Support brianvu-tp

    (@briantp)

    Hi John,

    Thank you so much for your kind feedback!

    We’re delighted to hear that the provided solution works perfectly for you. Should you have any further questions or need additional assistance, don’t hesitate to reach out. We’re always here to help!

    Best regards,
    Brianvu-tp

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Course Arhive default sort’ is closed to new replies.