Singular view of multiple posts
- This topic has 5 replies, 2 voices, and was last updated 3 years, 11 months ago by
Long Nguyen.
-
AuthorPosts
-
May 1, 2021 at 3:28 AM #27796
Igor Gouvêa
ParticipantHi
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/tbj2DXCAnnex 2 shows the type of post "Equipamento"
https://cutt.ly/6bj2UTZAttachment 3 shows the result of the view I need:
https://cutt.ly/Cbj2RlAI've been with this question for 1 week, I've read all the documentation and I can't find a way out.
May 2, 2021 at 9:04 AM #27815Long Nguyen
ModeratorHi 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:
- Get the ID of the current "Equipamento" by using the function get_queried_object_id()
- Query to get the post "Diário de Obra" and metadata. Get more details on the documentation https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters
$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".
May 3, 2021 at 4:34 AM #27836Igor Gouvêa
ParticipantThanks 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>
May 3, 2021 at 10:45 PM #27852Long Nguyen
ModeratorHi,
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/
May 5, 2021 at 3:00 AM #27911Igor Gouvêa
ParticipantHi
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>'; } }
May 5, 2021 at 9:34 AM #27920Long Nguyen
ModeratorHi 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/ -
AuthorPosts
- You must be logged in to reply to this topic.