• I am trying to create one page which will, when loaded from one of multiple end-points, list out all of the custom posts in a specific category:

    Custom post type category would be ‘topic-1’, and the url/endpoint print out all of the posts in that category would be ‘/discover-health/topics/topic-1/’

    I have a dynamic end point set up so that any /discover-health/topics/* is redirected to a specific page template (page-1234.php). Here is the function responsible for that:

    add_action('init', 'chat_topic_rewrite_rule');
    function chat_topic_rewrite_rule() {
    	global $wp;
    	$wp->add_query_var('chat_topic');
    
    	add_rewrite_rule('discover-health\/topic\/(.*)','index.php?page_id=1234&chat_topic=$matches[1]','top');
    }

    The code in that page-1234.php file to print out the custom posts in any specific category:

    <?php echo 'query is:' . 'post_type=chat_post&category_name=' . get_query_var('chat_topic') . '&posts_per_page=10'; ?><br/>
    <?php $my_query = new WP_Query('post_type=chat_post&category_name=' . get_query_var('chat_topic')); ?>
    <?php $posts = wp_get_recent_posts( array ('post_type' => 'recipe', 'numberposts' => 5, 'post_status' => "publish") ); ?>
    <?php foreach ( $posts as $post ?>
        <?php echo "hello line" ?><br>
        <?php echo $post['post_title'] . ' : ' .$post['post_content']; ?><br>
    <?php endwhile; ?>

    I have tried multiple different ways of restricting the query to one category, but the query always returns nothing if a category filter is present(There is definitely a post with that category topic-1 of this custom post type)

    I tried this as well, same thing, works perfect until I add the category filter into the query:

    <?php $posts = wp_get_recent_posts( array ('post_type' => 'chat_post', 'cat' => '187', 'numberposts' => 5, 'post_status' => "publish") );?>
    <?php foreach ( $posts as $post) : ?>
      <?php echo "hello line" ?><br>
      <?php echo $post['post_title'] . ' : ' .$post['post_content']; ?><br>
    <?php endforeach; ?>

    Like I mentioned, if I want to query all posts of that custom post type it works, but if I try to restrict it to the category in the url, it fails. I know the query string should be correct because it prints out: query is:post_type=chat_post&category_name=topic-1&posts_per_page=10

    Does the url-rewrite conflict with a second loop? I use custom post types and category filtering elsewhere on the site and it seems to work fine.

    If not then is there something visibly wrong with my code? Any help regarding this is greatly appreciated!

    For the curious, the url for this page is http://tmurraywellness.com/discover-health/topic/topic-1/

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)

The topic ‘Secondary loop in custom post type dysfunctional’ is closed to new replies.