Support

Account

Home Forums General Issues Query by Post_Object

Solved

Query by Post_Object

  • Appreciate any and all help:

    I have a Custom Post Type “Drivers”. On the Single Template, I want to query other Post Types using ACF post_object, named ‘drivers’ to identify and display only the matching posts. I am using a multiple select post_object, set to return the post_object.

    My current attempts are not returning any results and I need some guidance. Here is my the current code I am using:

    
    <?php 
    $args = array(
    		'post_type'		=> 'post',
    		'posts_per_page'	=> -1,
    		'orderby' => 'date',
    		'order' => 'DESC',
    		'meta_query' => array(
    			array(
    				'key' => 'drivers',
    				'value' => '"' . get_the_ID() . '"',
    				'compare' => 'LIKE'
    				)
    		)
    
    );
    
    // query
    $wp_query = new WP_Query( $args );
    
    // loop
    while( $wp_query->have_posts() )
    {
    $wp_query->the_post();
    the_title();
    // ...
    }?>
    
    

    Thanks in advance for your help!

  • Not sure if you still need help with this, trying to clear up some older questions.

    If I understand correctly, on you site the “post” post type has a custom field on it that is named “drivers” and this field is a relationship field.

    If this is true then what you have should work. The only thing I can come up with looking at your code is that you’re either querying the wrong post type or that the field name is not “drivers”

  • Hi John, thanks. I was able to get this working by changing the field display to ID rather than Post Name.

  • Your code not working fine

  • <?php 
    $post_id = get_the_ID();
    $args = array(
    	'post_type'	=> 'post',
    	'posts_per_page'=> -1,
    	'meta_query'	=> array(
    	array(
    		'key' => 'attraction_in',
    		'value' =>  $post_id,
    		'compare' => 'IN'
    		)
    	)
    );
    
    // query
    $wp_query = new WP_Query( $args );
    if ($wp_query->have_posts() ) :
    	echo '<div class="single-destination-list-items">';
    	echo '<h3>Top attractions in '.get_the_title( $post_id ).'</h3>';
    	echo '<ul>';
    while( $wp_query->have_posts() ) { $wp_query->the_post();
    ?>
    <li>
    <a href="<?php the_permalink(); ?>"><?php echo get_the_post_thumbnail( $page->ID, 'tours' ); ?></a>
    <div class="items-right">
    <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
    </div>
    </li>
    <?php
    }
    echo '</ul>';
    echo '</div>';
    endif;
    ?>

    I did use this code, but not working fine, please help me .

    Thanks

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

The topic ‘Query by Post_Object’ is closed to new replies.