0

I'm loading custom post type to different pages by changing cat id and sometimes post type name

query_posts(array(
 'post_type' => 'posttype_name','posts_per_page'=>-1,
  'tax_query' => array(
  array(
   'taxonomy' => 'type',
    'terms' => cat_id    )// soups
     ))
  );

I want to change the "posttype_name" and "cat_id" using a advanced custom field, something like below so I can generate more pages using 1 templates for different posttypes. Below method is not working, I also tried assigning a variable to the cf and add the variable to the array, no results. I may be doing something wrong.

'taxonomy' => 'the_field('posttype_name');'

Soo far no luck, can any share some idea how to impliment this

0

Firstly, don't use query_posts()

It should be noted that using this to replace the main query on a page can increase page loading times, in worst case scenarios more than doubling the amount of work needed or more. While easy to use, the function is also prone to confusion and problems later on. See the note further below on caveats for details.

https://developer.wordpress.org/reference/functions/query_posts/#more-information

Secondly, it really sounds like you're taking the wrong approach here. When you say:

so I can generate more pages using 1 templates for different posttypes.

Do you realise WordPress automatically creates pages for post type and taxonomy archives? You just need to have the post type and taxonomy be public. WordPress will pick the appropriate template for these pages based on the Template Hierarchy.

But regarding using ACF fields in query arguments (whether in query_posts() or WP_Query), the issue is two things:

  1. You need to use a function that returns a value, rather than outputting it to the screen. See the answers to this Stack Overflow question for more information on the difference.
  2. You're trying to use a function inside a string (text between quotes '').

Since Advanced Custom Fields' function for returning a field value is get_field(), you would use it like this:

'taxonomy' => get_field( 'posttype_name' ),

Pay close attention to where ', ;, and , are used, compared to your attempt.

  • Dear Jacob, thanks for the quick reply, I'm aware of the category templates, but in this case, there are other elements on the page unique to different items so I wanna control them manually. I tried your line of code still no value. but thanks again taking time to answer this. – Roshan Deshapriya Mar 28 '18 at 9:42
  • Dear Jacob, it worked, it was a ' making null values. cheers. – Roshan Deshapriya Mar 28 '18 at 9:50

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.