0

I have two custom post types: events and venue. I'm trying to query related events into the venue page. I have the related variable stored in a custom field for venues and the events related value in the_content(). Can I pull the_content() from events before I run new WP_Query() and if so why isn't this working?

$args_up = array(
    'post_type'         => 'events', 
    'posts_per_page'    => 20,
    'paged'             => $paged, 
    'meta_query'        => array(
        array(
            'key'       => the_content(),
            'value'     => $related_venue_variable,
            'compare'   => '=='
        )
    )   
); 
0

the_content() echoes data to the page so essentially your query looks like:

'meta_query' => array(
    array(
        'key'     => '',
        'value'   => $related_venue_variable,
        'compare' => '=='
     )
)

You want get_the_content() to return a string that you can pass along through the query.

However, having the post content as a meta key doesn't make any sense. For one, that key only holds 225 characters. Given that you may be storing the key from another post in the post content of a second post, that may not be a problem, but any filters applied on the post content will be as those can alter the content retrieved. In short, what you are doing is a bad, bad idea.

Now I'm guessing, but I think this is close to what you want to achieve:

if( have_posts() ) {
    while( have_posts() ) {
        the_post();

        $args_up = array(
            'post_type'         => 'events', 
            'posts_per_page'    => 20,
            'paged'             => $paged, 
            'meta_query'        => array(
                array(
                    'key'       => $post->post_content,
                    'value'     => $related_venue_variable,
                    'compare'   => '=='
                )
            )
        );

        $eq = new WP_Query( $args_up );
    }
}

But it's still a bad idea to save a key as post content. You'd be better off saving it as custom meta in both posts. Of course, there might be a better way to do this all around if I knew exactly how it all went together.

| improve this answer | |
  • thanks for the response. Your suggestion did not work but it got me thinking in the right direction thank you. – Tsea Dec 15 '15 at 21:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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