WordPress.org

Ready to get started?Download WordPress

Forums

Query All Posts From A Taxonomy (11 posts)

  1. alanchrishughes
    Member
    Posted 2 years ago #

    Such a simple question but there really isn't anything at all out there about this. Simply querying all posts that have been tagged to terms from a certain taxonomy?

    Like "Food" and querying "Chinese" or "American" is very simple, but to simply grab all posts tagged somewhere as "Food" how would you go about that?

  2. keesiemeijer
    moderator
    Posted 2 years ago #

    Maybe with something like this [untested]:

    <?php
      $terms = get_terms( 'food', 'fields=ids' );
      $args = array(
        'tax_query' => array(
          array(
          'taxonomy' => 'food',
          'field' => 'id',
          'terms' => $terms
          )
        )
      );
      query_posts($args);
    ?>
  3. alanchrishughes
    Member
    Posted 2 years ago #

    Thank you. That didn't return anything though.

  4. keesiemeijer
    moderator
    Posted 2 years ago #

    Is "food" the correct taxonomy name? Check in your theme's functions.php what the taxonomy name is that you registered.
    http://codex.wordpress.org/Function_Reference/register_taxonomy

  5. alanchrishughes
    Member
    Posted 2 years ago #

    Yes, it is correct. I've tried it with a few other existing taxonomies as well with the same results.

  6. keesiemeijer
    moderator
    Posted 2 years ago #

    test with this and see if it returns an array with term id's from the taxonomy food or a WP_Error Object:

    $terms = get_terms( 'food', 'fields=ids' );
    echo '<pre>';
    print_r($terms);
    echo '</pre>';
  7. alanchrishughes
    Member
    Posted 2 years ago #

    It returned

    Array
    (
    [0] => 22
    [1] => 24
    )

  8. alanchrishughes
    Member
    Posted 2 years ago #

    I don't know if that is what you were looking for or not or what that even means lol

  9. alanchrishughes
    Member
    Posted 2 years ago #

    Is there anything I was suppose to change or add to your original code? I just directly copied and pasted what you have there.

  10. keesiemeijer
    moderator
    Posted 2 years ago #

    Wel it finds terms from the "food taxonomy" so it should work. Can you paste and submit the full code of the template file with the query_posts in it into a pastebin.com and post the link to it here? see the Forum Rules for posting code and using the pastebin.

  11. alanchrishughes
    Member
    Posted 2 years ago #

    This didn't seem like it was going to work so I reluctantly just switched to doing everything by category. It would still be nice to know how to do this if it is in fact possible.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.