• Hello,

    I’m trying to create a custom user registration form used for Educational website.

    Is it possible to automatically retrieve categories of posts (custom posts) in the registration form.

    The form should be:

    username :
    password :
    first name:
    last name :

    optional

    <category list> (courses list)

    if user choose <category 1> it will automatically show related sub-categories example:

    <course1>
    – <sub-course-1>
    – <lesson 1>
    – <lesson 2>
    – <sub-course-2>
    – <lesson 1>
    – <lesson 2>

    Is there a way to do such thing into registration form?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    You can easily get all categories in a dropdown with wp_dropdown_categories(). Any children are displayed as well, below their parent and indented a bit IIRC, This would include categories not assigned to your custom post type though.

    To get only categories assigned to your CPT, you can use wp_get_object_terms(). You’ll have to loop through the results to generate the dropdown HTML. This function will require an array of all CPT IDs. There’s a number of ways to get such an array, the most efficient would probably be using $wpdb->get_col()

    To only display child categories after the parent is selected would be handled with javascript. The entire category tree could be defined as a javascript array and referred to when the user selects a parent. If this array is really large, it may become more efficient to query the server via AJAX for the children of the selected category.

    Just about anything is possible, but except for the all categories dropdown, the solution will require some coding work to accomplish.

    Thread Starter c05338

    (@c05338)

    @bcworkz thank you for your reply and these valuable information.

    Is it possible to retrieve categories of custom post called “courses”?

    And please can you advice with an example script to do the process.

    Thank you again!

    Moderator bcworkz

    (@bcworkz)

    Yes! The following (untested) script ends up with an array of category term objects in $terms. They represent all category terms which are applied to all published ‘courses’ post types.

    //$wpd->prepare() is normally required here, except we are not using any variables in our query
    $courses = $wpdb->get_col("SELECT ID FROM $wpdb->posts WHERE post_status ='publish' AND post_type ='courses';", 'ARRAY_N');
    $terms = wp_get_object_terms( $courses, 'category');

    You would then need to loop through each term object to generate a dropdown form field. Unfortunately, this by itself would not organize the terms hierarchically, we cannot even order the results by parent. To get the organization you want, in the first loop, parse the terms into another array structure where the keys are parent IDs.

    Step through the parent 0 (top level) terms in the new array. For each term, output an option block, then loop through any child terms, which are keyed to the top level term’s ID. It is this loop from which the children dropdown option blocks are generated. Something like this, very much untested:

    $sorted = array();
    foreach ( $terms as $term ) {
       $sorted[$term->parent][] = $term;
    }
    echo "<select>";
    foreach ( $sorted[0] as $term ) {
       echo "<option value='{$term->term_id}'>{$term->name}</option>";
       foreach ( $sorted[$term->term_id] as $child ) {
          echo "<option value='{$child->term_id}'>- {$child->name}</option>";
       }
    }
    echo "</select>";

    Don’t be surprised if this does not work on first attempt. Some tweaking and debugging is likely required. Still, I believe the underlying logic is sound.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Categories List into Registration Form’ is closed to new replies.