WordPress.org

Ready to get started?Download WordPress

Forums

Custom Post Type Category Query (7 posts)

  1. AJ Troxell
    Member
    Posted 3 years 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 3 years 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. AJ Troxell
    Member
    Posted 3 years ago #

    Good call! Thanks a ton!

  4. AJ Troxell
    Member
    Posted 3 years 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 3 years 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. AJ Troxell
    Member
    Posted 3 years 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. AJ Troxell
    Member
    Posted 3 years 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.

Topic Closed

This topic has been closed to new replies.

About this Topic