1

I'm trying to make the following mock up with WordPress: https://imgur.com/a/Sem7y

I created a custom post type and I created two categories (general and filling instructions). I used ACF plugin to create custom fields for the post type: https://imgur.com/a/o2QJV

However, when I create the loop I can't get it to show posts for only the general category, it will show any category. Also, it only shows one post (most recent), I want it to show all of them for the one category.

        <ul class="pdfLinks">
                <?php

                    //The Arguments
                    $args = array(
                        'posts_per_page' => 50,
                        'post_type' => 'documents',
                        'category_name' => 'general'
                    );

                    //The Query
                    $genral_documents = new WP_Query ( $args ); ?>

                <?php

                    //If we have the posts...
                    if (the_field ('title')) :
                        while(the_field('title')) :
                            the_field('title');
                    endwhile;
                    endif;
                ?>
                    <?php
                        $pdf1 = get_field('pdf_1');
                    ?>
                        <a class="download_button" target="_blank" href="<?php echo $pdf1['url']; ?>">
                            <li>
                                Download File
                            </li>
                        </a>
                </ul> 

I'm very new to coding for WordPress, so I'm not sure what I'm doing wrong. Thank you for your help :)

  • 2
    The WP_Query category_* parameters only work for the built-in Categories ( used for posts ). I'm assuming you have a Custom Taxonomy and thus need to use a tax_query. – Howdy_McGee Jan 31 '18 at 21:33
  • I chose category for the taxonomy type under ACF imgur.com/a/9UXdL @Howdy_McGee – Andrea G Jan 31 '18 at 21:34
  • 1
    Advanced Custom Fields saves it's data as postmeta. You'll need to do a meta_query or one of the other meta_* properties of WP_Query. You also have Term ID as the selected value so you won't be able to use the slug. – Howdy_McGee Jan 31 '18 at 21:38
  • I tried using the built in WordPress categories also though and that still didn't work. Sorry, what is Term ID? Also, do you know why it is only showing one post? I created a few, but it only displays the most recent post. Thanks @Howdy_McGee – Andrea G Jan 31 '18 at 21:41
  • it seems that you are not using the loop; have you read codex.wordpress.org/Class_Reference/WP_Query#Standard_Loop – Michael Feb 1 '18 at 0:24
0

Try this:

`

                $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

                $genral_args = array(
                    'posts_per_page' => '50',
                    'post_type' => 'documents',
                    'cat' => 'your-category-id',
                    'paged' => $paged
                );

                $genral_query = new WP_Query( $genral_args );

                while ( $genral_query->have_posts() ) : $genral_query->the_post(); ?>

            <?php

                //If we have the posts...
                if (the_field ('title')) :
                    while(the_field('title')) :
                        the_field('title');
                endwhile;
                endif;
            ?>
                <?php
                    $pdf1 = get_field('pdf_1');
                ?>
                    <a class="download_button" target="_blank" href="<?php echo $pdf1['url']; ?>">
                        <li>
                            Download File
                        </li>
                    </a>

                    <?php endwhile; wp_reset_postdata(); ?>
            </ul> `

(change the "your-category-id" to id, or if you want choose more category, use thus: 'cat' => 'your-category-id, your-category-id2')

  • Hi, that didn't work. It still shows a post from a different category and it only shows one post instead of the 4 I have created. – Andrea G Jan 31 '18 at 22:34
  • Ahh, sorry, add '' to 50, thus '50' – Galgóczi Levente Jan 31 '18 at 22:38
  • That didn't work either. @Galgóczi Levente – Andrea G Feb 1 '18 at 0:36
  • please check my answer (I dont tested, maybe you need delete this: <?php //If we have the posts... if (the_field ('title')) : while(the_field('title')) : the_field('title'); endwhile; endif; ?>) – Galgóczi Levente Feb 1 '18 at 1:03
  • Hi, I figured it out, another commenter pointed out that I didn't have the loop written properly. codex.wordpress.org/Class_Reference/WP_Query#Standard_Loop – Andrea G Feb 1 '18 at 1:33
0

Try This:

<?php
$taxonomy="category";
$postData = new WP_Query(
  array(
    'post_type' => 'documents',
    'posts_per_page'=> 50,
    'post_status' => 'publish',
    'tax_query' => array(
      array(
        'taxonomy' => $taxonomy, // your custom taxonomy name here
        'field' => 'slug',  // you can specify field like id,name and slug.
        'terms' => 'general' // you can specify category id, name and slug
      )
  ) 
));
if($postData->have_posts()):
  while ($postData->have_posts()): $postData->the_post();
    echo the_title()."<br>";
    $pdf1 = get_field('pdf_1'); ?>

    <a class="download_button" target="_blank" href="<?php echo $pdf1['url']; ?>">
      <li>
          Download File
      </li>
    </a>
  <?php endwhile;
  wp_reset_postdata();
endif; ?>

Thanks

  • It is always better to add an explanation why an answer would solve the problem - thank you. – Nicolai Feb 1 '18 at 13:27
  • That worked but why does this happen? imgur.com/a/msSFn – Andrea G Feb 1 '18 at 15:12

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.