Support

Account

Home Forums General Issues Insert Cat ID into Query Reply To: Insert Cat ID into Query

  • Hi @skasprick

    any ACF function beginning with “the_” echoes out the result. Replace it with “get_” and it will instead return the result.

    That’s why you’re seeing the ID echoed on the page but not applied to your query.

    So do this:

    
    query_posts(array(
        'cat' => get_sub_field('part_category'), // get posts by category id
        'posts_per_page' => -1 // all posts
    ));
    ?>
    

    and it should work better.

    I do feel the need to inform you that using query_posts is a nono in the WP developer community since quite some time. If you’re looking to alter a core query you should take a look at the pre_get_posts filter: https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts

    and If you’re looking to create a whole new set of posts you should look at wp_query: codex.wordpress.org/Class_Reference/WP_Query