Search code examples
phpwordpressvariablescustom-post-typeadvanced-custom-fields

variable as Post_type in wp_query - Wordpress


I have a question regarding Wordpress and selective post type selection.

I am building a template but I am trying to make a function with ACF that when someone makes a page they can select which post type they want to show on this page.

I made a variable with the following in it:

$p = the_field('post_typer');

This variable is getting his value when a user is making a page and selects which post type he wants to show.

 $p = the_field('post_typer');

    // WP_Query arguments
    $args = array(
        'post_type' =>  $p,
);

I am wondering why this is not working:

'post_type' =>  $p,

Please help


Solution

  • Depending on the type of field you have, you should be doing get_field

    $p = get_field('post_typer');
    
    // WP_Query arguments
    $args = array(
        'post_type' =>  $p,
    );
    

    https://www.advancedcustomfields.com/resources/get_field/