-2

I have a meta_key with name director in my posts and i tried to show all posts with some value like christian bale in director meta_key, i want to use this query in single.php and not showing Current post in new query

Thanks for any help

1
  • You can create common category for both of them and then assign to both .Check condition when displaying post that christan bell category is aasigned to movie posttype and then call that all movie posts that are in same category. – Sonali Feb 28 '17 at 12:56
2

If you want to get the post for an actor for example, there is different solutions, but in my opinion, the best is to use tag as actors names, or post meta. The current solution I give to you is using WordPress search system and try to find posts.

// We try to get post for Christian bale as a keyword
$args = array(
    'post_type'     => 'post',
    'post__not_in'  => array(get_the_ID()), // We don't need the current post
    's'             => 'christian bale', // We put the Christian Bale search here
);

// Or as a meta value
$args = array(
    'post_type'     => 'post',
    'post__not_in'  => array(get_the_ID()), // We don't need the current post
    'meta_key'   => 'director',
    'meta_value' => 'christian bale',
);

$films = new WP_Query($args);

// If there's posts, show it
if($films->have_posts())
{
    while($films->have_posts())
    {
        $films->the_post();
        the_title();
        the_content();      
    }
}

// Reset query
wp_reset_query();

Using this after your post, or in your post will allow you to show post containing "christian bale" text.

8
  • how to use search in custom feilds ? example search in custom feilds {director} and how to search post title not just christian bale – alam7o Feb 28 '17 at 12:59
  • If your custom field is a string, the best is to change the $args. I'll update my post. – Elex Feb 28 '17 at 13:01
  • Answer post is updated ! – Elex Feb 28 '17 at 13:04
  • thanks for good answer how to change christian bale in 'meta_value' => 'christian bale', with post title ? i want used code in single php for all posts – alam7o Feb 28 '17 at 13:06
  • 'meta_value' => 'christian bale', will be 'meta_value' => get_the_title(), – Elex Feb 28 '17 at 13:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.