WordPress.org

Forums

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

  1. Jay1990
    Member
    Posted 1 year 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); ?>
    
    <?php
    $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>
    </div>
  2. vtxyzzy
    Member
    Posted 1 year ago #

    Try using 'category_name' instead of 'cat'.

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

Topic Closed

This topic has been closed to new replies.

About this Topic