Support

Account

Home Forums General Issues meta_key query with Post Object field

Solving

meta_key query with Post Object field

  • Hi

    I am trying to work out how to use a meta_query with the Post Object field. Here is the code

    
    $id = get_the_ID();
    
    $args = array(
    						'numberposts' => -1,
    						'orderby' => 'date',
    						'order' => 'DESC',
    						'post_type' => 'reviews',
    						'meta_key' => 'review_tour',
    						'meta_value' => '"'.$id.'"'
    					);

    review_tour is the name of the Post Object field. However this is an array and I need to get there ID from the array, so essentially ‘meta_key’ => ‘review_tour[ID]’ but am not sure how this is done, any ideas?

    Thanks

  • Hi @phantomdentist

    If the value is saved as an array, you can treat the field the same as a relationship field. This means, you can follow this tutorial for your post_object field:

    http://www.advancedcustomfields.com/resources/tutorials/querying-relationship-fields/

  • hi,

    i’m having the same problem. i can’t get my query to pull information that i’ve set up using a post object from a separate post type. i’m including the code below.

    any help would be greatly appreciated. 🙂

    <?php
    global $post;
    $args = array( 'post_type' => 'listing', 'posts_per_page' => -1, 'meta_query' => array( array( 'key' => 'sold_value', 'value' => 'Yes' ), array( 'key' => 'salesperson_name', 'value' => $salesperson = $_GET['salesperson'] ) ) );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) : setup_postdata($post);
    //Get the listing values
    include (ABSPATH . 'assets/dealer-functions/dealer-functions.php'); ?>
    <tr>
    <td class="vehicle-year center"><?php echo $man_year; ?></td>
    <td class="vehicle-title"><?php the_title(); ?></td>
    <td class="vehicle-stock right"><?php echo $car_stock; ?></td>
    <td class="vehicle-vin center"><?php echo $car_vin; ?></td>
    <td class="vehicle-acquired center"><?php echo $car_regdate; ?></td>
    <td class="vehicle-sold center"><?php echo $car_sale_date; ?></td>
    <td class="vehicle-list right"><?php if(empty($car_list_price)) { echo 'No Price'; } else { ?><?php if (is_numeric($car_list_price)) { echo number_format($car_list_price, 2, '.', get_option('wp_thousandseparator')); } else { echo $car_list_price; } ?><?php } ?></td>
    <td class="vehicle-base right"><?php if(empty($car_basecost)) { echo 'No Base'; } else { ?><?php if (is_numeric($car_basecost)) { echo number_format($car_basecost, 2, '.', get_option('wp_thousandseparator')); } else { echo $car_basecost; } ?><?php } ?></td>
    <td class="vehicle-sale right"><?php if(empty($car_sale_price)) { echo 'No Price'; } else { ?><?php if (is_numeric($car_sale_price)) { echo number_format($car_sale_price, 2, '.', get_option('wp_thousandseparator')); } else { echo $car_sale_price; } ?><?php } ?></td>
    <td class="vehicle-salesperson center"><?php if(!empty($get_salesperson)) { echo $car_salesperson; } else { echo 'None'; } ?></td>
    </tr>
    <?php endforeach; ?>
    </table>

    —-and—-

    array (
    'key' => 'field_5356425f36c39',
    'label' => 'Salesperson Name',
    'name' => 'salesperson_name',
    'type' => 'post_object',
    'post_type' => array (
    0 => 'salesrep',
    ),
    'taxonomy' => array (
    0 => 'all',
    ),
    'allow_null' => 1,
    'multiple' => 0,
    ),

  • nevermind. figured out how to make it work. it was passing through the ID of the page and not the title which is what i had been querying for. i just converted the form field back into the ID to run the query and now it’s working. 🙂

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘meta_key query with Post Object field’ is closed to new replies.