Skip to content

Get Contents of pick column when pick is a custom post type

bonsak edited this page Nov 18, 2012 · 2 revisions

The below code shows an easy way to make a WordPress page template retrieve a specific pod item and display a list of related products (Custom Post Type) chosen via a Pods PICK column.

In this example, we have a service that we would like to show all products associated with the service. This was originally built so that I can assign MarketPress products to Pods "Services".

    <?php
    // register the pod for 'My Services'
    $service = new Pod('my_services');
    // Limit to only getting services that are associated with the permalink in the url (detail_url) 
    $params = array(
                    'where' => 'permalink = "' . pods_var(-1, 'url') . '"',
                    'limit' => -1
            ); 
    $service->find( $params );
    // Get the name of the service we want to show
    $service_name = $service_name = $service->get_field('name');
    //Get the field that contains our products 
    $service_prod = $service_prod = $service->get_field('associated_products');
    ?> 
    <h2><?php echo $service_name; ?></h2>
    <div id="associated-products">
    <?php  
    //Loop through each item selected in our associated products field as a WordPress post
    foreach ( $service_prod as $post ) { 
    //Define the $name variable as the title of the post
    $name = $post[ 'post_title' ]; 
    //Display the name
    echo $name;
    }
    ?>