1

I'm building a cinema site, I came with a problem, let me explain:

  • Movies- are stored in Post's
  • Actors- are stored in Post Type - persoane ( taxonomy= lista)
  • Serials - are stored in Post Type - serial ( taxonomy= seriale)

I use Advance Custom Field to link the Person to the movie -

enter image description here

AFC (http://www.advancedcustomfields.com/)

How I add:
I add new actor post to post type ( persoane), after i add the movie (in post's), in sidebar(post's, movie page) I link the actor:

enter image description here

After I link the actor on the actor page it display the movie's that actor play in

enter image description here

I use this code to do that

<?php 
//create new loop query for films by this actor
$movies = $wpdb->get_col("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = 'distributie' AND meta_value LIKE '%".get_the_ID()."%'");
$the_query = new WP_Query( array( 'post__in' => $movies) );

// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
    echo '<div class="actor-box-related"><a class="actor-titlu" href="';
    the_permalink();
    echo '" title="'.get_the_title().'">';
    ?>
    <img class="actor-img" src="/scripts/timthumb.php?src=<?php the_field('img'); ?>&h=90&w=60&zc=1" alt="<?php the_title(); ?>"  title="<?php the_title(); ?>"/>

    <?php the_title();
    echo '</a></div>';
endwhile;

// Reset Post Data
wp_reset_postdata();
?>

The problem:

I create a Post Type - serial where I add the Serials (Ex: heroes,prisonbreak... )

How can i display on the actor's page the serials that actor play in. The custom_field for serials is "distributie_serial" - i change in that code but no sucess, it did not work.

The up code get me only the movies from post's, how can i get the movies from that PostType (serial).

Can I put both together to get movies and serials? Or how can i display the serial's on actor page, and IF no movie or serial is atach retrive the text "No movies for this actor"

Thanks in advance for those who help me.

6
  • 1
    I see you're using timthumb.php, considering the major security risks of using it, I'd advise you use add_image_size and the_post_thumbnailinstead. Also your 'distributie' post meta should be a custom taxonomy called 'distributie'.
    – Tom J Nowell
    Commented Feb 4, 2013 at 12:14
  • distributie - is the custom field where i ad the actors in post's page.
    – Batman
    Commented Feb 4, 2013 at 12:17
  • You should use a custom taxonomy not a custom field. A taxonomy UI will be provided automatically for you to select from
    – Tom J Nowell
    Commented Feb 4, 2013 at 12:19
  • And if i will go with the custom field, how can i do that, my site is build now i canot chage entire site, Tom J Nowell , anny solution to get the serial from that PostType ? I think i have to change somting in that link $movies = $wpdb->get_col("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = 'distributie' AND meta_value LIKE '%".get_the_ID()."%'");
    – Batman
    Commented Feb 6, 2013 at 2:41
  • What does var_dump($movies); gives you? An array?
    – brasofilo
    Commented Feb 6, 2013 at 21:46

2 Answers 2

0

Hy,

i resolve a problem, a liitle question, how can i get the text if nothing is selected in custom field ( distributie_serial). Ex: no serial for this actor.

This is the Code that works.

<?php 
$seriale = get_posts(array('post_type' => 'serial',
     'meta_query' => array(
                     array('key' => 'distributie_serial', // name of custom field
                           'value' => '"' . get_the_ID() . '"',
                           'compare' => 'LIKE'))
                        ));
?>
<?php if( $seriale ): ?>
<?php foreach( $seriale as $serial ): ?>
<div class="actor-box-related">
<a class="actor-titlu" href="<?php echo get_permalink( $serial->ID ); ?>">
<img class="actor-img" src="/scripts/timthumb.php?src=<?php echo get_field('imagine_serial', $serial->ID); ?>&h=90&w=60&zc=1" alt="<?php echo $photo['alt']; ?>" />
<?php echo get_the_title( $serial->ID ); ?>
</a>
</div>

<?php endforeach; ?>
<?php endif; ?>
1
  • 1
    <?php endforeach; else : // text when there's no posts endif; ?>
    – Maikal
    Commented Sep 4, 2015 at 15:41
0
<?php 
  $seriale = get_posts(array('post_type' => 'serial',
 'meta_query' => array(
                 array('key' => 'distributie_serial', // name of custom field
                       'value' => '"' . get_the_ID() . '"',
                       'compare' => 'LIKE'))
                    ));
?>
<?php if( $seriale ): ?>
<?php foreach( $seriale as $serial ): ?>
<div class="actor-box-related">
<a class="actor-titlu" href="<?php echo get_permalink( $serial->ID ); ?      >">
<img class="actor-img" src="/scripts/timthumb.php?src=<?php echo     get_field('imagine_serial', $serial->ID); ?>&h=90&w=60&zc=1" alt="<?php     echo $photo['alt']; ?>" />
<?php echo get_the_title( $serial->ID ); ?>
</a>
</div>

<?php endforeach; ?>
<?php else: 
echo 'Sorry! No serial for this actor.';
?>
<?php endif; ?>`
2
  • Hi! It would be great if you can also explain the code.
    – bravokeyl
    Commented Aug 5, 2016 at 7:32
  • If $serial is not empty, you posts will be displayed, if not, the else part gets executed. is this what you are not clear about?
    – Annapurna
    Commented Aug 5, 2016 at 7:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.