How to get the number of childs

Support General How to get the number of childsResolved

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #38676
    Guillaume LORAINGuillaume LORAIN
    Participant

    Hello.

    I would like to display the number of connected elements in a relashionship.
    For exemple I have a custom post type for classes and another one for students. Students are affected to classes thanks to a relashionship. I would like to be able to display how many students are connected for every class.
    Classs A : 25 students, Class B : 18 students; Class C : 28 students...

    Is it possible ?
    Thanks

    #38678
    Guillaume LORAINGuillaume LORAIN
    Participant

    I succeeded to find by myself.

    Here is the code to use : {{relationship.to|length }}

    #38679
    Long NguyenLong Nguyen
    Moderator

    Hi,

    When creating the WP Query to get relation posts, you can get the number of posts by using the property found_posts. Read more on the documentation https://developer.wordpress.org/reference/classes/wp_query/#properties
    https://docs.metabox.io/extensions/mb-relationships/#getting-connected-items

    // The Query
    $query = new WP_Query( $args );
    $total = $query->found_posts;
    #38688
    Guillaume LORAINGuillaume LORAIN
    Participant

    Thanks

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.