1

I want to display a certain category from WordPress post type, but it is not showing...

Here is my quote. it displays all posts instead of "wedding venue" category post.

<?php
    $args = array(
    'post_type'      => 'post',
    'category_name'  => 'wedding-venue',
    'posts_per_page' => 8,
    'facetwp' => true,
);
    $query = new WP_Query( $args );
?>

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>


//loop content here

<?php endwhile; endif; ?>
<?php wp_reset_postdata(); ?>
2
  • What is facetwp argument for? Is the category slug correct?
    – bravokeyl
    Jul 15, 2018 at 5:36
  • That is to code to enable the facetwp.com filter
    – wayne
    Jul 15, 2018 at 8:31

1 Answer 1

1

Of course it displays all posts... You create your own custom WP_Query, but then you ignore it and use global one ;)

Here’s the correct code:

<?php
    $args = array(
    'post_type'      => 'post',
    'category_name'  => 'wedding-venue',
    'posts_per_page' => 8,
    'facetwp' => true,
);
    $query = new WP_Query( $args );
?>

<?php if ( $query->have_posts() ) : while ( $query-> have_posts() ) : $query-> the_post(); ?>


//loop content here

<?php
    endwhile;
    wp_reset_postdata(); // this should be inside if - there is no need to rested postdata if the_post hasn’t been called.
    endif; 
?>

PS. Remember, that if you want to modify posts that are displayed in main loop, you should use pre_get_posts action - creating your own query is redundant...

2
  • Hi Krzysiek, Thanks! that is work. The only issue is the facetwp filtering plugin not working, I think I have to check with facetwp filter plugin developer on this. Thanks again :)
    – wayne
    Jul 15, 2018 at 8:19
  • really appreciate it
    – wayne
    Jul 15, 2018 at 8:32

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.