Forums

Custom Post Type Category Query (7 posts)

  1. phiredesign
    Member
    Posted 11 months ago #

    In my category.php page, I am currently using the below code to call the posts:
    <?php $loop = new WP_Query(array('post_type'=>'menu', 'taxonomy'=>'category', 'term' => 'dinnerappetizers', 'orderby' => 'menu_order', 'order' => 'asc')); ?>

    However, I would like this query to contain a variable for the "term" depending on which category a user navigates to. I have been through site after site and nothing works out.

    If a user clicks on Lunch Appetizers, they are taken to the category.php with the term lunchappetizers term in the query, and if Dinner Appetizers, are taken to the category.php with dinnerappetizers as the term in the query.

  2. jimmyt1988
    Member
    Posted 11 months ago #

    in the anchor tag, put some url parameters:

    <a href = "<?php echo get_bloginfo( 'url' ) . '/dinner-appetizers?term=dinnerappetizers'; ?>">Dinner appetizers</a>

    then on the category.php do a url call:

    <?php $currentTerm = $_GET[ 'term' ]; ?>

    and stuff that into your query:

    <?php $loop = new WP_Query(array('post_type'=>'menu', 'taxonomy'=>'category', 'term' => $currentTerm, 'orderby' => 'menu_order', 'order' => 'asc')); ?>

  3. phiredesign
    Member
    Posted 11 months ago #

    Good call! Thanks a ton!

  4. phiredesign
    Member
    Posted 11 months ago #

    Whenever I do it... it calls all posts from within the custom post type for some reason.
    I am using:

    <?php $currentTerm = $_GET[ 'term' ]; ?>
    <?php $loop = new WP_Query(array('post_type'=>'menu', 'taxonomy'=>'category', 'term' => $currentTerm, 'orderby' => 'menu_order', 'order' => 'asc')); ?>
    <?php while ($loop->have_posts()) : $loop->the_post();  ?>
  5. jimmyt1988
    Member
    Posted 11 months ago #

    The problem is in the WP_query... might need to re-learn:

    http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

    I might be able to help later. @work atm.

  6. phiredesign
    Member
    Posted 11 months ago #

    I have changed it around a bit:

    <?php $currentTerm = $_GET[ 'term' ]; ?>
    
    <?php $query = new WP_Query( array( 'post_type'=>'menu', 'taxonomy'=>'category', 'term' => $currentTerm, 'orderby' => 'menu_order', 'order' => 'asc' ) ); ?>
    
    <?php while ($query->have_posts()) : $query->the_post(); ?>

    Still to no avail... it's still showing ALL posts from all terms.

  7. phiredesign
    Member
    Posted 11 months ago #

    Got the example that jimmyt1988 gave, working... amazing as it is, it seems a bit cumbersome. Is there a way to do this without having to add anything to the nav links? This prevents the user from being able to add category/terms in the long run.

Reply

You must log in to post.

About this Topic