• Resolved dansz

    (@dansz)


    I’ve added a select advanced box used to associate one custom post type item with other items of a different type.

    For example, for a certain INDUSTRY the clients would recommend certain SERVICES.

    Here’s how I’ve registered it to show under industry:

    array(
    'name'        => __( 'Services we recommend', 'your-prefix' ),
    'id'          => "{$prefix}service_rec",
    'type'        => 'post',
    'post_type'   => 'service',
    'field_type'  => 'select_advanced',
    'placeholder' => __( 'Select an Item', 'your-prefix' ),
    'multiple'    => true,
    'query_args'  => array(
        'post_status'    => 'publish',
        'posts_per_page' => - 1,
    ),
    ),

    And I’m showing it on the front end like this:

    $services = rwmb_meta('as_service_rec');
    if ( !empty($services) ) {
    echo '<ul>';
    foreach ($services as $service) {
    $page = get_post($service);
    echo '<li>' .  $page->post_title . '</li>';
    }
    echo '</ul>';
    }

    The problem is that this works successfully on any single industry page, but the array shows up EMPTY (testing with print_r) when I try to include this in a wp_query loop elsewhere on the site.

    Driving me crazy!

    https://ww.wp.xz.cn/plugins/meta-box/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Anh Tran

    (@rilwis)

    Can you show the code for the query loop?

    Thread Starter dansz

    (@dansz)

    Yes sir

    $args = array(
    'post_type' => array('service'),
    'post_status' => 'publish',
    'posts_per_page' => -1
    );
    
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) :
    $my_query->the_post();
    the_title();
    $services = rwmb_meta('as_service_rec');
    print_r($services);
    echo '<br>';
    endwhile;
    }
    wp_reset_query();

    I think i just answered my own question the query should be INDUSTRY not SERVICE

    ugh!

    So the correct code would be:

    $args = array(
    'post_type' => array('industry'),
    'post_status' => 'publish',
    'posts_per_page' => -1
    );
    
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
    while ($my_query->have_posts()) :
    $my_query->the_post();
    the_title();
    $services = rwmb_meta('as_service_rec');
    print_r($services);
    echo '<br>';
    endwhile;
    }
    wp_reset_query();

    Plugin Author Anh Tran

    (@rilwis)

    Glad to see you resolved the problem 🙂

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Array shows up in some instance, empty in others’ is closed to new replies.