Support

Account

Home Forums General Issues Query post with get_field()

Solved

Query post with get_field()

  • Hi, i have a doubt

    I have a post type called “playlists”, a select field called “categorias”

    I want to show playlists posts if the “categorias” value is the same as the current post

    I tried this code but filter by value did not work

    
    <?php
    
    $categoria = get_field( "categorias" );
    
    // args
    $args = array(
    	'numberposts'	=> -1,
    	'post_type'		=> 'playlists',
    	'meta_query'	=> array(
    		'relation'		=> 'OR',
    		array(
    			'key'		=> 'categorias',
    			'value'		=>  $categoria,
    			'compare'	=> 'LIKE'
    		)
    	)
    );
    
    // query
    $the_query = new WP_Query( $args );
    
    ?>
    <?php if( $the_query->have_posts() ): ?>
    	<ul>
    	<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    		<li>
    			<a href="<?php the_permalink(); ?>">
    				<img src="<?php the_post_thumbnail('thumbnail'); ?>" />
    				
    			</a>
    		</li>
    	<?php endwhile; ?>
    	</ul>
    <?php endif; ?>
    
    <?php wp_reset_query();	 // Restore global post data stomped by the_post(). ?>
    
  • Not sure if this is best practice, but it seems to me that it would work to simply query post_type = playlists, as you are doing, but without the meta_query. Instead, inside the loop, include:

    $currentCat = get_field('categorias', $current_id);
    if (get_field('categorias') == $currentCat) {...}

    where the $current_id is captured outside the loop.

  • Hi, Kaitlyn
    thanks, but I’m not know what’s still wrong

    
    <?php 
    
    if ( is_singular( 'playlists' ) ): 
    
    	if( have_posts() ): ?>
    
    		<ul>
    			
    		<?php while ( have_posts() ) : the_post(); 
    
    			$current_id = get_the_ID(); 
    			$currentCat = get_field('categorias', $current_id);
    			if (get_field('categorias') == $currentCat): ?>
    			
    				<li>
    					<a href="<?php the_permalink(); ?>">
    						<img src="<?php the_post_thumbnail('thumbnail'); ?>" />
    
    					</a>
    				</li>
    			
    			<?php endif; ?>
    			
    		<?php endwhile; ?>
    			
    		</ul>
    
    	<?php endif; // have_post ?>
    
    <?php endif; //is_singular ?>
    
    
  • You need to capture the current ID outside of the loop, otherwise get_the_id() will grab the ID of the post it is currently querying.

    Try moving that chunk of code before the while, e.g.:

    <?php
      if ( is_singular( 'playlists' ) ):
        $current_category = get_field( 'categorias', get_the_ID() );
        if ( have_posts() ):
    ?>
  • Sorry, I try this, but still not work.

    
    <?php
    
    if ( is_singular( 'playlists' ) ):
    
     	$current_category = get_field( 'categorias', get_the_ID() );
    
    	if( have_posts() ): ?>
    
    		<ul>
    
    		<?php while ( have_posts() ) : the_post();
    
    			if (get_field('categorias') == $current_category): ?>
    
    				<li>
    					<a href="<?php the_permalink(); ?>">
    						<img src="<?php the_post_thumbnail('thumbnail'); ?>" />
    
    					</a>
    				</li>
    
    			<?php endif; ?>
    
    		<?php endwhile; ?>
    
    		</ul>
    
    	<?php endif; // have_post ?>
    
    <?php endif; //is_singular ?>
    
  • Is “categorias” a select field or a taxonomy field?

    Does the field allow only one selection or multiple selections?

  • Hi, John

    It’ s a select field allowing multiple selections

  • Select fields are saved as serialized arrays. That means that this

    
    $categoria = get_field( "categorias" );
    

    will return an array and you want to search for other posts that contain one of the values in the array. What you need is something like this

    
    $categoria = get_field( "categorias" );
    
    $meta_query = array('relation' => 'OR');
    if ($categoria) {
    	foreach ($categoria as $value) {
    		$meta_query[] = array(
    		  'key' => 'categorias',
    			'value' => '"'.$value.'"',
    			'compare'	=> 'LIKE'
    		);
    	}
    }
    
    // args
    $args = array(
    	'numberposts'	=> -1,
    	'post_type'		=> 'playlists',
    	'meta_query'	=> $meta_query
    );
    
Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.