• Resolved John

    (@artoftech)


    Hello,
    I’m looking to extend the sorting capability (on the courses archive page) beyond the options:

    Newly published
    Title a-z, z-a
    Price l-h, h-l
    Popular

    Is it possible to add sorting on a tag?
    Thanks in advance,
    John

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

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

    (@briantp)

    Hi John,

    Thank you for reaching out to us!

    Regarding your request to extend the sorting capability on the courses archive page, this functionality is available with LearnPress. However, the implementation process can differ depending on the theme you’re using.

    Could you kindly let us know which theme you are currently using? If you’re using one of our premium themes (e.g., Eduma), it would be helpful if you could also provide the name of the demo you’re using. This will allow us to give you the most accurate guidance tailored to your setup.

    Looking forward to your response!

    Best regards,
    Brianvu-tp

    Thread Starter John

    (@artoftech)

    Dear Brianvu-tp,

    Thanks for the response. We are using Astra (not Pro)…

    Best,
    John

    Plugin Support brianvu-tp

    (@briantp)

    Hi John,

    Thank you for your prompt response!

    You can filter courses by Course Tag using the LearnPress – Course Filter widget. Here’s how you can set it up:

    1. Go to Appearance > Widgets in your WordPress dashboard.
    2. Locate the LearnPress – Course Filter widget.
    3. Drag and drop it into the All Courses widget area.
    4. In the widget settings, enable the Course Tag option.

    Once set up, the sidebar on your All Courses page will display the course tags, allowing students to filter courses based on their tags easily.

    If you have any further questions or need assistance with this setup, feel free to let us know!

    Best regards,
    Brianvu-tp

    Thread Starter John

    (@artoftech)

    Hi,
    That’s good but I have already got tags in the filter. What I want is to add one to the sort which is not in the widget, but in the dropdown, on the actual page (not the sidebar) under the title, next to the switch to grid/list toggle. Does that make sense?

    Many thanks,
    John

    Plugin Support brianvu-tp

    (@briantp)

    Hi John,

    Thank you for your clarification!

    Since tags are assigned to courses, we’re curious about how you envision sorting by tags. Do you have a specific order or criteria in mind for sorting by tags?

    Your input would help us better understand your requirements, allowing us to provide you with the most accurate and effective solution.

    Looking forward to your thoughts!

    Best regards,
    Brianvu-tp

    Thread Starter John

    (@artoftech)

    Hi and yes – my video courses correspond to specific pages in a companion book. I have tagged each course with that page number. It would be therefore nice to be able to sort the courses based on the page number so students can easily find a corresponding video while referencing the book. I hope that makes sense…

    I found where I can add an option to the dropdown, but haven’t been able to discover the code where the query to the database is taking place when someone clicks on the sort option.

    Thanks again for your time,
    John

    Plugin Support brianvu-tp

    (@briantp)

    Hi John,

    Thank you for providing additional details!

    You can refer to the learn-press/courses/order-by/values hook, which is located in the file:
    wp-content/plugins/learnpress/inc/TemplateHooks/Course/ListCoursesTemplate.php.

    Using this hook, you can customize the sorting options in your child theme to include your desired “Sort by Tag” functionality. This should allow you to sort courses based on the page number tags you’ve assigned.

    If you have any questions or need further assistance implementing this, feel free to reach out!

    Best regards,
    Brianvu-tp

    Thread Starter John

    (@artoftech)

    Thank you Brianvu-tp! I will let you know if I run into any issues.

    Best,
    John

    Thread Starter John

    (@artoftech)

    Hi again, so I got this code implemented starting at line 519:

    	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',
    [
    'tags' => esc_html__( 'Page Number', 'learnpress' ),

    and that has added the option in the dropdown – great!
    Sorry to be dense, but I don’t see where a query is built other than for the html_count_students() function which has some SQL for getting the total students of a given category. Where do I hook to modify the query which is I assume being run in the render_courses function to add the new tag order by requirement?

    Thanks for your support!

    Plugin Support brianvu-tp

    (@briantp)

    Hi John,

    Thank you for getting back to us with your progress!

    To implement the query for your custom “Sort by Tag” functionality, you can refer to the following hook: apply_filters( ‘lp/courses/filter’, $filter );

    This hook is located in the file:
    wp-content/plugins/learnpress/inc/Models/Courses.php.

    Within this file, you will find examples for sorting by Title A-Z (post_title) and Title Z-A (post_title_desc). Using this hook and the existing query examples, you can implement your custom sort logic for tags in the functions.php file of your child theme. This approach ensures that your customizations are preserved during plugin updates.

    If you encounter any challenges or have further questions, feel free to reach out—we’re here to assist!

    Best regards,
    Brianvu-tp

    Thread Starter John

    (@artoftech)

    For anyone following along in the future, I got the new option added and the pricing ones removed (since all our courses are free) using the hook:

    function oht_create_new_tag_sort_filter( $arr ) {
    $arr = array(
    //New sort option
    'tags' => esc_html__( 'Page Number', 'learnpress' ),
    //Remaining used sort options
    'post_date' => esc_html__( 'Newly published', 'learnpress' ),
    'post_title' => esc_html__( 'Title a-z', 'learnpress' ),
    'post_title_desc' => esc_html__( 'Title z-a', 'learnpress' ),
    'popular' => esc_html__( 'Popular', 'learnpress' )
    );
    return $arr;
    }
    add_filter( 'learn-press/courses/order-by/values', 'oht_create_new_tag_sort_filter', 10, 1 );

    Let me know if this isn’t what you had in mind. I will look at the query hook when I get more time and report back.
    Thanks for your support!
    John

    Plugin Support brianvu-tp

    (@briantp)

    Hi John,

    Thank you so much for sharing your progress and the detailed solution!

    We truly appreciate your efforts in contributing back to the community, as your code will undoubtedly be helpful for others looking to implement similar functionality. Please feel free to reach out if you have any further questions or updates—we’re always here to assist!

    Best regards,
    Brianvu-tp

    Thread Starter John

    (@artoftech)

    Brianvu-tp,
    No worries! I am on to the query part now and need some further clues. It’s a bit hard to pull out the full query with all the joins but my intended functionality based on the tag sort option is this:


    SELECT * FROM wpp9_terms
    WHERE slug LIKE 'page-%'
    ORDER BY SUBSTR(slug,6,2)+0;
    --note str to num coercion to get 1 before 10, etc...

    Which produces the ordered list of tags based on the prefix “page-“:


    | term_id | name | slug | term_group |
    |---------|-----------|---------|------------|
    | 270 | Page: #3 | page-3 | 0 |
    | 269 | Page: #4 | page-4 | 0 |
    | 271 | Page: #6 | page-6 | 0 |
    | 277 | Page: #7 | page-7 | 0 |
    | 261 | Page: #8 | page-8 | 0 |
    | 263 | Page: #9 | page-9 | 0 |
    | 265 | Page: #10 | page-10 | 0 |
    | 268 | Page: #12 | page-12 | 0 |
    | 280 | Page: #14 | page-14 | 0 |
    | 283 | Page: #15 | page-15 | 0 |
    | 337 | Page: #16 | page-16 | 0 |
    | 274 | Page: #17 | page-17 | 0 |
    | 336 | Page: #18 | page-18 | 0 |
    | 333 | Page: #19 | page-19 | 0 |
    | 338 | Page: #20 | page-20 | 0 |
    | 335 | Page: #21 | page-21 | 0 |
    | 339 | Page: #22 | page-22 | 0 |
    | 106 | Page: #23 | page-23 | 0 |
    | 287 | Page: #24 | page-24 | 0 |
    | 289 | Page: #26 | page-26 | 0 |
    | 290 | Page: #27 | page-27 | 0 |

    This is what would order the courses output when sorting on the new page number option. I think I see where to hook the order by (inc/Models/Courses.php: function get_courses) but I think there is more setup that needs to happen b/c simply adding a switch statement:

    case 'tag_ids':
    $filter->order_by = $filter->tag_ids;
    $filter->order = 'ASC';
    break;

    doesn’t achieve anything but a ‘no records found’…

    Thanks again for your help,
    John

    • This reply was modified 3 months ago by John.
    Thread Starter John

    (@artoftech)

    To follow up I pieced the actual query I need together. It goes like this:


    SELECT posts.id, posts.post_title, terms.name from
    wpp9_posts as posts
    INNER JOIN wpp9_term_relationships as rel
    ON posts.id = rel.object_id
    INNER JOIN wpp9_terms as terms
    ON rel.term_taxonomy_id = terms.term_id
    WHERE posts.post_type='lp_course'
    AND terms.slug LIKE 'page-%'
    ORDER BY SUBSTR(terms.slug,6,2)+0;

    I used only the columns necessary to prove the theory.

    [ Please do not bump. ]

    Thread Starter John

    (@artoftech)

    Sorry, wasn’t aware bumping was a no-no, but it appears this is stalled and I think we’re very close. Any updates?

    Thanks again!

Viewing 15 replies - 1 through 15 (of 21 total)
  • You must be logged in to reply to this topic.