Support Forum
Hi
I need to make a view of the singular type that shows the data related to another type of entry.
In the post type "Diário de Obra", I insert data from "Equipamento" (title and quantity)
When I go to see the singular of "Equipamentos" I want to show the Diários de Obra where this equipamentos appears.
Annex 1 shows the type of post "Diário de Obra":
https://cutt.ly/tbj2DXC
Annex 2 shows the type of post "Equipamento"
https://cutt.ly/6bj2UTZ
Attachment 3 shows the result of the view I need:
https://cutt.ly/Cbj2RlA
I've been with this question for 1 week, I've read all the documentation and I can't find a way out.
Hi Igor,
To get the post "Diário de Obra" that has the ID of a post "Equipamentos", you can use the meta query. Here are some guides for you by using PHP code:
$args = array(
'post_type' => 'Diário-de-Obra-slug', //change the CPT slug
'meta_key' => 'field_post_id', //change the field post ID
'meta_value' => get_queried_object_id(),
'meta_compare' => '=',
);
$query = new WP_Query( $args );
In the loop, you can show the post title and any post meta of the post "Diário de Obra".
Thanks Long
I tried to run this php like this and print. I got it.
Do you have an example of how to run a php inside the view and print the variable?
<div class="row">
<div class="col-md-12" style="page-break-inside:avoid">
<script>
$args = array(
'post_type' => 'diario-de-obra',
'meta_key' => 'equipamento',
'meta_value' => get_queried_object_id(),
'meta_compare' => '=',
);
$query = new WP_Query( $args );
</script>
{{ $query }}
</div>
</div>
Hi,
Please refer to this topic to know how to run a PHP function in the View https://support.metabox.io/topic/integration-with-search-filter-pro/
Hi
I'm getting to the goal. Just one more detail:
My meta-key is a group. See the image:
In my code I put the group name in meta_key but meta_value is a json.
How do I specify that the meta_value is "equipamento"?
function equipamentos() {
$args = array(
'post_type' => 'diario-de-obra',
'meta_key' => 'grupo-equipamentos',
'meta_value' => get_queried_object_id(),
'meta_compare' => '=',
);
$query = new WP_Query( $args );
while ( $query->have_posts() ) {
$query->the_post();
echo get_the_title() . '<br>';
}
}
Hi Igor,
The field type group
is used to store value, not for querying. We do not encourage to query by subfields in a group due to it's very difficult and complicated. Please get more information on these articles
https://metabox.io/create-group-of-custom-fields-with-meta-box-group/
https://metabox.io/searchwp-metabox-integration/