0

I have a custom post type called Authors where you can choose an author for that post (authors don't have user account so I went down the custom post type route).

author select

I am building an "author profile" page (outside of the loop) and I want to get all posts that this author has been chosen against.

I've written this function to attempt to get these posts when passing the post ID of the author:

function authorPosts($authorID)
{
    $args = array(
        'post_type'     => 'post',
        'post_status'   => 'publish',
        'meta_key'      => 'author',
        'meta_value'    => $authorID
    );

    $getPosts = new WP_Query($args);
    $posts = FALSE;

    foreach($getPosts->posts as $post)
    {
        $posts[] = array(
            'ID'            => $post->ID,
            'image'         => $this->getFeaturedImageUrl($post->ID, 'medium'),
            'post_title'    => $post->post_title,
            'post_name'     => $post->post_name
        );    
    }
    return $posts;
}

This just seems to return all of my posts. When passing 13118 (author ID)

This is the meta data for a test post showing it has "Alex Crooks" assigned to it (post ID 13118). Is it because it's being stored as json instead of a string? Is there a way to query a meta value when it's stored as json?

post meta

  • 1
    Should we be seeing 13118 somewhere in your screenshot? – GhostToast Jun 3 '13 at 13:54
  • Whoops. I see it in the array on meta_id 6921! I do wonder though if this is not returning what you want because the meta_value holds a serialized array, and not just a string? Can you save it as a string instead? – GhostToast Jun 3 '13 at 14:02
  • Please explain: You have a CPT called authors which you seem to be using as a taxonomy but are checking the post meta for the data? – s_ha_dum Jun 3 '13 at 14:02
  • That's right, however I set the authors up as a "relationship" too? Used custom fields plugin to help me do it: bit.ly/1aW2qlP It created that nice search box for the custom post type on the normal post creation screen. (screenshot 1) – Titan Jun 3 '13 at 14:47
  • So is there no way it can search json stored values? – Titan Jun 4 '13 at 8:35

Your Answer

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

Browse other questions tagged or ask your own question.