1

我希望课程页面能够按价格查询课程...找到了这一点,并希望它能完成工作...

内容存档 course.php

$default_order = apply_filters( 'thim_default_order_course_option', array(
    'newly-published' => esc_html__( 'Newly published', 'eduma' ),
    'alphabetical'    => esc_html__( 'Alphabetical', 'eduma' ),
    'most-members'    => esc_html__( 'Most members', 'eduma' ),
    //'price'             => esc_html__( 'Price', 'eduma' )
) );;

还位:learnpress_v3_functions.php

if ( !function_exists( 'thim_course_order_query' ) ) {
    function thim_course_order_query( $query ) {
        if ( !$query->is_main_query() ) {
            return;
        }

        if ( !is_post_type_archive( 'lp_course' ) && !is_tax( 'course_category' ) ) {
            return;
        }

        // Sort
        if ( isset( $_POST['course_orderby'] ) ) {
            switch ( $_POST['course_orderby'] ) {
                case 'alphabetical':
                    $query->set( 'orderby', 'title' );
                    $query->set( 'order', 'ASC' );

                    break;
                case 'most-members':
                    $query->set( 'orderby', 'meta_value_num' );
                    $query->set( 'meta_key', 'thim_real_student_enrolled' );
                    $query->set( 'order', 'DESC' );

                    break;
                case 'price':
                    $query->set( 'orderby', 'price' );
                    $query->set( 'order', 'ASC' );
                default:
                    $query->set( 'orderby', 'date' );
                    $query->set( 'order', 'DESC' );
            }
        }

任何帮助表示赞赏。

4

2 回答 2

0

在文件中:content-archive-course.php

$default_order = apply_filters( 'thim_default_order_course_option', array(
'newly-published' => esc_html__( 'Newly published', 'eduma' ),
'alphabetical'    => esc_html__( 'Alphabetical', 'eduma' ),
'most-members'    => esc_html__( 'Most members', 'eduma' ),
'priceasc'        => esc_html__( 'Prices: low to High', 'eduma' ),
'pricedesc'       => esc_html__( 'Prices: high to low', 'eduma' ),

) );;

在你的functions.php文件中添加这个钩子:

/*Add new filter options to course archive page */
add_action( 'pre_get_posts', 'thim_course_order_query', 15);

function thim_course_order_query( $query ) {
        if ( !$query->is_main_query() ) {
            return;
        }

        if ( !is_post_type_archive( 'lp_course' ) && !is_tax( 'course_category' ) ) {
            return;
        }

        // Sort
        if ( isset( $_POST['course_orderby'] ) ) {

            switch ( $_POST['course_orderby'] ) {
                case 'alphabetical':
                    $query->set( 'orderby', 'title' );
                    $query->set( 'order', 'ASC' );

                    break;
                case 'most-members':
                    $query->set( 'orderby', 'meta_value_num' );
                    $query->set( 'meta_key', 'thim_real_student_enrolled' );
                    $query->set( 'order', 'DESC' );

                    break;
                case 'priceasc':
                    $query->set( 'orderby', 'meta_value_num' );
                    $query->set( 'meta_key', '_lp_price' );
                    $query->set( 'order', 'ASC' );

                    break;
                case 'pricedesc':
                    $query->set( 'orderby', 'meta_value_num' );
                    $query->set( 'meta_key', '_lp_price' );
                    $query->set( 'order', 'DESC' );

                    break;
                default:
                    $query->set( 'orderby', 'date' );
                    $query->set( 'order', 'DESC' );
            }
        }

        // Pagination
        if ( isset( $_POST['course_paged'] ) ) {
            $query->set( 'paged', $_POST['course_paged'] );
        }

        // Filter by categories
        if ( isset( $_POST['course_cate_filter'] ) && is_array( $_POST['course_cate_filter'] ) ) {
            $query->set( 'tax_query', array(
                array(
                    'taxonomy' => 'course_category',
                    'field'    => 'term_id',
                    'terms'    => $_POST['course_cate_filter'],
                )
            ) );
            //$query->set( 'posts_per_page', - 1 );
        }

        // Filter by instructor
        if ( isset( $_POST['course_instructor_filter'] ) && is_array( $_POST['course_instructor_filter'] ) ) {
            $query->set( 'author__in', $_POST['course_instructor_filter'] );
        }

        // Filter by price
        // TODO query courses has sale price
        if ( isset( $_POST['course_price_filter'] ) ) {
            switch ( $_POST['course_price_filter'] ) {
                case 'free':
                    $query->set( 'meta_query', array(
                        array(
                            'key'     => '_lp_price',
                            'compare' => 'NOT EXISTS'
                        )
                    ) );
                    break;
                case 'paid':
                    $query->set( 'meta_query', array(
                        array(
                            'key'     => '_lp_price',
                            'compare' => 'EXISTS'
                        )
                    ) );
                    break;
                case 'all':
                    break;

                default:

            }
        }
    }
于 2020-08-20T10:46:51.840 回答
-1

//内容存档课程

$default_order = apply_filters( 'thim_default_order_course_option', array(
    'alphabetical'    => esc_html__( 'Alphabetical', 'eduma' ),
    'newly-published' => esc_html__( 'Newly published', 'eduma' ),
    'priceasc'        => esc_html__( 'Most Asc', 'eduma' ),
    'pricedesc'   => esc_html__( 'Most Desc', 'eduma' ),
) );;

//learnpress-v3-functions

                case 'priceasc':
                    $query->set( 'orderby', 'price' );
                    $query->set( 'order', 'ASC' );

                    break;
                case 'pricedesc':
                    $query->set( 'orderby', 'price' );
                    $query->set( 'order', 'DESC' );

                    break;
于 2020-07-02T15:11:50.030 回答