0

I would like to know how to print a variable inside a custom WP_Query.

I have the following code:

<?php
$args = array (
  'post_type' => 'post',
  'posts_per_page' =>3,
  'orderby' => 'date',
  'order' => 'DESC',
);
$loop = new WP_Query( $args );
endwhile; 
wp_reset_query();

And I have a post type name value stored in a variable called $post_type_name.

However, when adding it to my code

<?php 
$post_type_name = get_sub_field('post_type_name'); 
$args = array (
  'post_type' => '$post_type_name',
  'posts_per_page' =>3, 
  'orderby' => 'date',
  'order' => 'DESC',
);
$loop = new WP_Query( $args );

endwhile; 
wp_reset_query();

the code breaks. Any ideas what could be wrong?

1

Remove the single quotation, use flowing code. hope it will work

'post_type' => $post_type_name,
  • Excellent, it was exactly what's missing! – Johann Oct 27 '16 at 12:57

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.