Home Forums Front-end Issues Display Custom Post by Select Choice in Custom Field


Display Custom Post by Select Choice in Custom Field

  • I have a custom post set up with a question, answer, and then the topic/category the question goes in. I would like to loop through the posts and only display the posts that are marked “bookstore” in a section and then in a separate section show the posts that are marked “education”. I have already written an array that pulls my info and then a loop. I can see the “topic/category” when I var dump the argument. Is there a way to pull only posts that meet a certain string?

        $questions = array();
        $args = array(
        'post_type' => 'faq',
        'posts_per_page' => -1,
        'order' => 'ASC',
    	$loop = new WP_Query( $args );
    	while ( $loop->have_posts() ) : $loop->the_post();
            $questions[] = array(
    	  'title' => get_the_title(),
    	  'answer' => get_field('answer'),
    	  'category' => get_field('question_category'),
  • What I would do is loop through the posts multiple times, once for each section that you want to display. Test the field to see if it should be shown. At the end of each loop use rewind_posts(), to reset to the first post.

  • Thank you 🙂
    I ended up calling each topic with a meta_key and then just ran the argument and loop for each topic. Feels like a ton of code, but it worked!


  • The main reason that I would get all the posts and loop through them multiple times is only because that way I’d only be doing one query, and additional queries will slow down the page. In the end the amount of code the way you do it may be slightly more.

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Display Custom Post by Select Choice in Custom Field’ is closed to new replies.