Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have two post types that I've associated in one direction by assigning Post A as a custom meta field of Post B. I need to search based on the titles of Post A and return the associated Post B results. This feels a little tricky.

Normally I can display titles from Post A like so:

        $post = get_post($post_id);
        $author_post_id = get_post_meta($post->ID, 'custom_author', true);
        $post_object = get_post( $author_post_id );
        echo get_the_title($post_object);

And I believe I'm using meta_query correctly to search Post B:

        $args = array(
            'post_type' => 'journal_entry',
            'meta_query' => array(
                array(
                    'key' => 'custom_author',
                    'compare' => 'LIKE'
                )
            )
         );
        $query = new WP_Query( $args );
        if ( $query->have_posts() ): while ( $query->have_posts() ) : $query->the_post();
            // Loop
        endwhile; endif;
        // Reset post data
        wp_reset_postdata();

But I think I need to shoehorn get_the_title into my meta_query value. I think. I'm a little turned around. Am I on the correct path?

share|improve this question
    
You should use the_* functions before get_the_* functions. Later normally (ok, sometimes) don't have filters attached. –  kaiser Sep 5 '12 at 0:53
add comment

1 Answer

up vote 0 down vote accepted

Try setting $title_search = get_the_title($post_object); and then using it in your query as 'value' => $title_search:

$args = array(
    'post_type' => 'journal_entry',
    'meta_query' => array(
        array(
            'key' => 'custom_author',
            'compare' => 'LIKE',
            'value' => $title_search
        )
    )
 );
share|improve this answer
    
stealthyninja, your suggestion appears to work. Something must be wrong with my loop though. I'm using the suggestion as-is, yielding nothing, where my loop contains the_title and the_content. However, doing print_r before WP_Query yields: Array ( [post_type] => journal_entry [meta_query] => Array ( [0] => Array ( [key] => custom_author [compare] => LIKE [value] => Sandso Person ) ) ), which appears to be entirely correct. –  bode well carapace Sep 5 '12 at 1:23
1  
Actually I think I'm going about this completely the wrong way. I cobbled together code to query a single post when I want to query all journal_entry posts. stealthyninja's solution works for a most recent entry, so I'm marking it correct. –  bode well carapace Sep 5 '12 at 2:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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