Dynamically insert code to custom post type loop

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.

Topic loop custom-taxonomy plugin-development custom-post-types Wordpress

Category Web


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,
    );
?>

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.