Home Forums General Issues Filter Posts by Custom Fields


Filter Posts by Custom Fields

  • So I have built a custom post type for my videos called tv. Some of the videos reference products using a custom field post object this is called ‘instruments’.

    I’d like to make the relevant videos from tv come up automatically on these products. So I figure I need to query this data and just match post IDs. I found some code and edited it but it’s not working, can someone advise me where I’m going wrong?

    Many Thanks

    $args = array(
    	'numberposts'	=> -1,
    	'post_type'		=> 'tv',
    	'meta_key'		=> 'instruments',
    	'meta_value'	=>  $post->ID
    $the_query = new WP_Query( $args );
    <?php if( $the_query->have_posts() ): ?>
    	<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
    			<a href="<?php the_permalink(); ?>">
    				<?php the_title(); ?>
    	<?php endwhile; ?>
    <?php endif; ?>
    <?php wp_reset_query();	 // Restore global post data stomped by the_post(). ?>
  • I am assuming from your explanation that the post object field allows multiple selections

    $args = array(
    	'numberposts'	=> -1,
    	'post_type'		=> 'tv',
    	'meta_key'		=> 'instruments',
    	'meta_value'	=>  '"'.$post->ID.'"'
    	'meta_compare' => 'LIKE'
  • Amazing! Thank you so much. Just missing a comma on the end of ‘meta_value’.

    Thanks again

  • sorry, I don’t have my IDE to tell me about errors like that here 😛

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

You must be logged in to reply to this topic.