WP Query with categories only shows one post and ignores the category

admin2025-01-07  3

I'm trying to make the following mock up with WordPress: .jpg

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: .jpg

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 :)

I'm trying to make the following mock up with WordPress: https://i.sstatic.net/wyYS1.jpg

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://i.sstatic.net/xBn3S.jpg

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 :)

Share Improve this question asked Jan 31, 2018 at 21:27 Andrea GAndrea G 211 silver badge2 bronze badges 6
  • 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 Commented Jan 31, 2018 at 21:33
  • I chose category for the taxonomy type under ACF imgur.com/a/9UXdL @Howdy_McGee – Andrea G Commented Jan 31, 2018 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 Commented Jan 31, 2018 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 Commented Jan 31, 2018 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 Commented Feb 1, 2018 at 0:24
 |  Show 1 more comment

2 Answers 2

Reset to default 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')

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

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736258694a533.html

最新回复(0)