Ready to get started?Download WordPress


wordpress show posts from post type and cat using slug (2 posts)

  1. Jay1990
    Posted 5 months ago #

    I am trying to show posts from the 'faq' custom post type that are tagged with the category name of the page your on. for example if you are on the 'family' page it should show all faq's tagged with the category 'family'. the only way it can do this is by using the url slug as i have many category's and pages. here is my code so far but this shows all posts from the post type.

    <?php $slug = preg_replace('/\s+/', '', $post->post_name); ?>
    $loop = new WP_Query( array(
    'post_type' => 'faq',
    'cat' => $slug,
    'posts_per_page' => 5,
    'orderby' => 'date',
    'order' => 'DESC'
    ) );
    while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <div class="related-faq">
    <a href="<?php the_permalink(); ?>"><?php echo wp_trim_words( get_the_title(), 5 ); ?></a>
  2. vtxyzzy
    Posted 5 months ago #

    Try using 'category_name' instead of 'cat'.

    In addition, use sanitize_title() instead of your preg_replace().


You must log in to post.

About this Topic