0

I need to display post from custom post type name "film", from a specific taxonomy "Horror." Everything work fine when I type the taxonomy directly like this:

    <?php  $blog = array(
    'post_type'         => 'film',
    'catfilm' => 'Horror',                      
    'post_status'       => 'publish',
    'posts_per_page'    =>4,
    'ignore_sticky_posts' => 1,
     );
    ?>
        

I need to insert dynamically the taxonomy with CMB2, so I change the code like this, but no luck. The post failed to appear:
My CMB2 function:

$cmb_tvseries->add_field( array(
    'name'        => esc_html__( 'type the category', 'cmb2' ),
    'id'          => 'mytheme_catfilm',
    'type'        => 'text',
) );

My loop change to:

  <?php $blog = array(
    'post_type'         => 'film',
    'catfilm' => '<?php echo ( get_post_meta( get_the_ID(), "mytheme_catfilm", true ) ); ?>',                       
    'post_status'       => 'publish',
    'posts_per_page'    =>4,
    'ignore_sticky_posts' => 1,
    );
    ?>

I am newbie on php. Please help.

1 Answer 1

1

Create a variable outside the array and then pass it to the array:

<?php 
$catfilm = get_post_meta( get_the_ID(), "mytheme_catfilm", true );
$blog = array(
    'post_type'         => 'film',
    'catfilm'           => $catfilm,                       
    'post_status'       => 'publish',
    'posts_per_page'    => 4,
    'ignore_sticky_posts' => 1,
    );
?>
1
  • It's work. Thank you very much.
    – Chl Lila
    May 8, 2022 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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