using php to display a list of custom posts and custom fields

Support MB Custom Post Type using php to display a list of custom posts and custom fields

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #36922
    jimimacjimimac
    Participant

    Hi Im trying to use php to display a list of a type of custom post. I created the post type using MB with an id of "course" and tried using the following php
    print_r(rwmb_meta('course'));

    It's not displaying anything so am i not meant to use rwmb_meta for posts?

    #36925
    Long NguyenLong Nguyen
    Moderator

    Hi,

    If you are using the helper function rwmb_meta() in a custom loop (not in the post template), please add the post ID to the third argument

    rwmb_meta( 'course', '', 123 )

    where 123 is the post ID. Please read more on the documentation https://docs.metabox.io/functions/rwmb-meta/

    #36949
    jimimacjimimac
    Participant

    Maybe i didnt explain too well. First I want to show a list of a particular post type not one particular post. So i want to show a list of posts of the type "course"

    #36964
    Long NguyenLong Nguyen
    Moderator

    Hi,

    To show a list of a post type, please use the custom query with WP Query and add the helper function rwmb_meta() in the loop to get the field value. For example:

    // The Query
    $the_query = new WP_Query( $args );
     
    // The Loop
    if ( $the_query->have_posts() ) {
        echo '<ul>';
        while ( $the_query->have_posts() ) {
            $the_query->the_post();
            echo '<li>' . get_the_title() . '</li>';
            echo rwmb_meta( 'course', '', get_the_ID() );
        }
        echo '</ul>';
    }
    wp_reset_postdata();

    Read more on the documentation https://developer.wordpress.org/reference/classes/wp_query/

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