GiulyM
GiulyM

Reputation: 41

WordPress how to query pages based on specific categories

I added the categories to Pages and I created a template to use for a specific Page. This Page have to show only the posts of 2 categories:

This is my code:

    <div class="container">

<div class="row">
<?php
  $pages = get_pages( array('category_name' => 'category1, category2' ) );
foreach ( $pages as $page ) {
?>
    <div class="col"> <a href="<?php echo get_page_link( $page->ID ) ?>" class="header-link"><?php echo $page->post_title ?></a>  <?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?>  </div>


    </div></div>

but it doesn't work, it shows all pages.

At the end I have to use a filter for these 2 categories, if I click "category1" it shows only category1 pages post, and if I click "category2" only category2 pages post. Thanks.

Upvotes: 1

Views: 1741

Answers (1)

Ruvee
Ruvee

Reputation: 9097

but it doesn't work, it shows all pages

Yes get_pages function accepts several arguments, however, 'category_name' is not one of them! Here's a list of arguments that get_pages recognizes: get_pagesDocs

  • 'post_type'
  • 'post_status'
  • 'child_of'
  • 'sort_order'
  • 'sort_column'
  • 'hierarchical'
  • 'exclude'
  • 'include'
  • 'meta_key'
  • 'meta_value'
  • 'authors'
  • 'parent'
  • 'exclude_tree'
  • 'number'
  • 'offset'

Now if you want to query your pages based on specific categories, then you could use wp_queryDocs.

  • Assuming that you're using the generic wordpress category not a custom category built by ACF and plugins of that sort!
  • Also, assuming you want to work with the slug of your categories.
$args = array(
  'post_type' => 'page',
  'posts_per_page' => -1,
  'post_status' => array('publish', 'private'),
  'tax_query' => array(
    array(
      'taxonomy' => 'category',
      'field'    => 'slug',
      'terms'    => array('test-category-01', 'test-category-02') // These are the slugs of the categories you're interested in
    )
  )
);

$all_pages = new WP_Query($args);

if ($all_pages) {
  while ($all_pages->have_posts()) {
    $all_pages->the_post(); ?>
    <div class="col">
      <a href="<?php echo get_page_link(get_the_ID()) ?>" class="header-link"><?php the_title() ?></a>
    </div>
<?php
  }
}

wp_reset_postdata();


Alternative way of performing the above query.

If you want to work with the id of your categories.

$args = array(
  'post_type' => 'page',
  'posts_per_page' => -1,
  'post_status' => array('publish', 'private'),
  'tax_query' => array(
    array(
      'taxonomy' => 'category',
      'field'    => 'term_id',
      'terms'    => array(4, 5), // These are the ids of the categories you're interested in
      'operator' => 'IN',
    ),
  )
);

$all_pages = new WP_Query($args);

if ($all_pages) {
  while ($all_pages->have_posts()) {
    $all_pages->the_post(); ?>
    <div class="col">
      <a href="<?php echo get_page_link(get_the_ID()) ?>" class="header-link"><?php the_title() ?></a>
    </div>
<?php
  }
}

wp_reset_postdata();

This answer has been fully tested on wordpress 5.8 and works seamlessly fine! Let me know if you have any other question(s).

Upvotes: 2

Related Questions