Create a Wordpress Query using ACF Relational Fields

Asked

Viewed 103 times

0

good night. I’m developing a movie registration website for a movie-aficionado cousin and need a help to put a query that lists the names of the films whose page personality participated or acted. I am developing (due to my little knowledge in programming) using Wordpress + Elementor + Custom Post Types UI + ACF. I created the following Cpts: Movies and Personalities In the Post Type Films, there are fields such as directors, actors, screenwriters, musicians and photographers that are filled in a relational way with the Post Type Personalities, which have their acting areas distinguished by taxonomy (director, actor, actress, screenwriter, musician and photography). So far everything is working perfectly. Now I would like it to be shown in the single of the personality, which films he acted. Searching in Google, and giving a lot of header in the wall and punch in knife tip, I arrived at the following code, that if possible was inserted in the single via shortcode. However even after a lot of research, today they complete almost 30 days, this function still doesn’t work. So far I managed to reach the following code:

add_shortcode ('filmografia', 'filmografia_ator' );

function filmografia() {
ob_start();

<?php
    $post_data = get_post( get_the_ID(),ARRAY_A );
    $post_data = $post_data['post_name'];

        if($post_data){
        $args = array(
                  'post_type'   => 'filmes',
              'meta_query'  => array(
                array( 
                      'key'     => 'atores',
                      'value'   => $post_data,
                      'compare' => '=',
                     ),
                        ),
                      'posts_per_page'=> -1
                );
        $query = new WP_Query( $args );

        // Loop da Listagem
        if ( $query->have_posts() ) { ?>

        <h3>Filmografia</h3>

        <ol>
        <?php while ( $query->have_posts() ) {
        $query->the_post(); ?>

        <li>
        <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> de <?php the_field('ano_lancamento'); ?> 

        </li>
        <?php } ?>
        </ol>

        <?php }
        wp_reset_postdata();
        }
        ?>
return ob_get_clean();
}

When I drafted this code I focused only on one type of performance and if it worked, I would move on to the second challenge that was to make a search for performance.

I believe the following flow should be observed: - Take the post ID (to identify which personality it is) - Take the post’s taxonomy (to identify what kind of action - director, actor, screenwriter, etc.) - Depending on the type of action, search for information in the corresponding field of post type films; - If there are post films whose performance has been defined: - Generate a loop in a sorted list with the movie name and year.

Could someone please give me a north? I thank everyone who has taken the time to read and help me in this fight.

1 answer

0

Oops, good afternoon, good afternoon!

Dude, seeing your shortcode will only be used on Cpt’s single? If so, there is no need for you to pass a query because the function will take the ID of the current post. For example, you could do something like this:

<?php 
function vv_filmes_by_actor()
{   
   $filmes = get_posts([
      'post_type'   => 'filmes',
      'meta_query'  => array(
         'key'     => 'atores',
         'value'   => get_the_ID(),
         'compare' => 'IN',
      ),
      'posts_per_page'=> -1
   ]);

   if( ! $filmes ) return "Nenhum filme encontrado";

   $html = '';

   foreach($filmes as $filme){
      $html .= '<li>'.$filme->post_title.'</li>';
   };

   return $html;
}
add_shorcode("vv_filmes_by_actor", "vv_filmes_by_actor");

Browser other questions tagged

You are not signed in. Login or sign up in order to post.