0

I have a section in my dashboard called "services".

  • I have 4 services
  • I have 2 categories for these services: residential and commercial
  • 2 of the 4 services have the category "residential", and the other 2 "commercial"

As it is now my services page is pulling up all 4 services.

Question: What code do I add, to show only the 2 services with the category of residential?

Here is my current code on the archive-service.php page:

$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
$args = array(
    'post_type' => 'service',
    'posts_per_page' => 8,
    'paged' => $paged,
);
$wp_query = new WP_Query($args);
while($wp_query->have_posts()) : $wp_query->the_post();
4
  • The WP_Query Codex page has a list of all query parameters with examples of how to use them.
    – Milo
    Commented Jan 23, 2016 at 17:53
  • Do I need to add a function to functions.php Commented Jan 23, 2016 at 20:05
  • From the above Codex page, I used this: 'tax_query' => array( array( 'taxonomy' => 'people', 'field' => 'slug', 'terms' => 'bob', ), ), But changed it to this: 'tax_query' => array( array( 'taxonomy' => 'people', 'field' => 'slug', 'terms' => 'bob', ), ), My final code: 'tax_query' => array( array( 'taxonomy' => 'people', 'field' => 'slug', 'terms' => 'bob', ), ), Thanks again.... Commented Jan 23, 2016 at 20:19
  • Add your solution as an answer, instead of a comment.
    – Gabriel
    Commented Jan 23, 2016 at 20:24

2 Answers 2

1

Add this to your args:

'taxonomies' => array( 'category' )

Should look like this-

$args = array(
    'post_type' => 'service',
    'posts_per_page' => 8,
    'paged' => $paged,
    'taxonomies' => array( 'category' ),
);
0

From the above Codex page, I used this:

'tax_query' => array( array( 'taxonomy' => 'people', 'field' => 'slug', 'terms' => 'bob', ), ),

But changed it to this:

'tax_query' => array( array( 'taxonomy' => 'category_service', 'field' => 'slug', 'terms' => 'residential', ), ),

My final code:

<?php 
            $paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
            $args = array(
                'post_type' => 'service',
                'posts_per_page' => 8,
                'paged' => $paged,
                'tax_query' => array(
                        array(
                            'taxonomy' => 'category_service',
                            'field'    => 'slug',
                            'terms'    => 'residential',
                        ),
                    ),

            );
            $wp_query = new WP_Query($args);
            while($wp_query->have_posts()) : $wp_query->the_post();
        ?>

Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.