Support

Account

Home Forums General Issues Adding field value into a php loop

Solving

Adding field value into a php loop

  • I have set up a field which I can fill in a custom post type id.
    I’m wanting to have the value of this placed into the following php loop:

    <?php 
    			$args = array( 'post_type' => 'the_field("post_type_name")', 'posts_per_page' => 99 );
    			$loop = new WP_Query( $args );
    			while ( $loop->have_posts() ) : $loop->the_post(); ?>

    The above works if I manually insert the custom post id ‘myPostType’, but not when using custom fields php.

    Can anyone advise where I’m going wrong…?

    Many thanks.

  • the_field() echos the value, you need to use get_field()

    
    $args = array( 'post_type' => 'get_field("post_type_name")', 'posts_per_page' => 99 );
    
  • Thanks for the response, I have tried the above but still failing. It will more than likely be my code… will look through. Many thanks., great plugin!

  • There are some extra quotes in what I posted

    
    $args = array('post_type' => get_field('post_type_name'), 'posts_per_page' => 99 );
    
Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Adding field value into a php loop’ is closed to new replies.