Support » Fixing WordPress » wp_query custom post type by custom field

  • So I have something like this..

    <ul>
    $custom_post_type_two_id = get_the_ID();
    $args = array(
    	'post_type' => 'custom_post_type_one',
    	'posts_per_page' => -1,
    	'meta_query' => array(
    		array(
    			'key' => 'custom_field_one',
    			'compare' => 'LIKE',
    			'value' => $custom_post_type_two_id,
    		)
    	)
    );
    $advanced_posts = new WP_Query( $args );
    if ($advanced_posts->have_posts()) :
    while ($advanced_posts->have_posts()) : $advanced_posts->the_post();
    echo '<li>' . the_field('custom_field_two') . '</li>';
    endwhile;
    endif;
    wp_reset_query();
    </ul>

    What I’m trying to accomplish is I don’t want the displayed list item custom field to be repeated. So I want it to dig through the ‘custom_post_type_one’ ‘custom_field_two’ posts and only display each unique field information list items once.

    So right now it may look like this..

    <ul>
    	<li>Car</li>
    	<li>Boat</li>
    	<li>Car</li>
    	<li>Truck</li>
    </ul>

    When I want it to do this..

    <ul>
    	<li>Car</li>
    	<li>Boat</li>
    	<li>Truck</li>
    </ul>

    Notice the extra ‘Car’ is gone, because it was repeated and not necessary.

  • The topic ‘wp_query custom post type by custom field’ is closed to new replies.