简体   繁体   中英

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

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/

This just worked for me:

        <?php 
        $tipo_post = get_field('tipo_de_post');
        $city_post = get_field('ciudad_npc');

        $loop = new WP_Query( 
              array( 
                'post_type' => $tipo_post, 
                'category_name' => $city_post, 
                'posts_per_page' => 700 
              )); 
          while ( $loop->have_posts() ) : $loop->the_post(); ?>
              <li class="td-nombre text-left align-middle"><a href="<?php the_permalink(); ?>"><?php the_field('nombre'); ?></a></li>
        <?php endwhile; ?>   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM