Custom Fields
Re-Envisioned
Support MB Views Singular view of multiple posts

  • Creator
    Topic
  • #27796
    Resolved Igor Gouvêa
    Participant

    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.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Author
    Replies
  • #27815
    Long Nguyen
    Moderator

    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".

    #27836
    Igor Gouvêa
    Participant

    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>
    #27852
    Long Nguyen
    Moderator

    Hi,

    Please refer to this topic to know how to run a PHP function in the View https://metabox.io/support/topic/integration-with-search-filter-pro/

    #27911
    Igor Gouvêa
    Participant

    Hi

    I'm getting to the goal. Just one more detail:

    My meta-key is a group. See the image:

    https://cutt.ly/ebny21z

    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>';
        }
    }
    
    #27920
    Long Nguyen
    Moderator

    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/

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.