Hey @tomwhita,
Can you please send the definition of _doc_project_association field? We need the code that creates the field, so we know how the value is stored in the database.
Container::make('post_meta', __('Document Information'))
->show_on_post_type('document')
->add_fields(array(
Field::make('text', 'doc_description', 'Description'),
Field::make('file', 'doc_file', 'File (PDF)'),
Field::make('association', 'doc_project_association', 'Assign To Project')
->set_types(array(
array(
'type' => 'post',
'post_type' => 'project',
),
)),
));
Howdy, @tomwhita,
It appears your meta_query should be like this:
$args = array(
'post_type' => array( 'document' ),
'post_status' => array( 'publish' ),
'meta_query' => array(
array(
'key' => '_doc_project_association',
'compare' => 'LIKE',
'value' => '"post:project:' . $post->ID . '"',
)
)
);
I’ve changed several things:
1. The post type is now document – previously it was post. This was necessary as I can see your container is registered on the document post type and not on post.
2. I’ve added 'compare' => 'LIKE' – this is necessary in order to make a full text search within the serialized value.
3. The value has been changed to '"post:project:' . $post->ID . '"' – this is how the association field values are saved in the database.
Please, let me know if you have further questions.
This is great. Thank you so much.
Just one final question…How do I echo the url of the file in ‘doc_file’?
I’ve already changed the field declaration to:
Field::make('file', 'doc_file', 'File (PDF)')
->set_value_type (url),
And I’m attempting to call it via:
$docs = get_posts($args);
foreach ($docs as $post) :
$doclink = carbon_get_post_meta ($post->ID,'doc_file');
setup_postdata($post);
?>
<div class="mdl-list__item">
<span class="mdl-list__item-primary-content"><i class="material-icons mdl-list__item-avatar ion-document-text"></i>
<span><a href=<?php echo $doclink; ?> target="_blank">
<?php the_title(); ?></a></span>
</span>
</div>
It outputs a link but its the link to the “project” post in the loop. I want to link to the file that associated with the project.
I suggest that you wrap the href attribute value in a pair of ". So instead of href=<?php echo $doclink; ?> you would have href="<?php echo $doclink; ?>".
Other than that, the code appears to be fine (considering that we don’t have your full code and we’re blindly debugging).
So it appears you don’t have a file uploaded to that document. In that case the link will be empty, resulting in a link to the current post (which is probably a project in your case).
Thanks again. I actually added the documents before changing the data setting and so they were still storing the ID. I updated them and everything works.
I really appreciate your help.
Great! Good luck with your project. 🙂