1

Hi i need get term ID from custom taxonomy but it is always NULL.

taxonomy-galerie-kategorie.php

<?php
    /**
     * A custom taxonomy template for gallery.
     */
?>
<?php Starkers_Utilities::get_template_parts( array( 'parts/shared/html-header', 'parts/shared/header' ) ); ?>

<?php Starkers_Utilities::get_template_parts( array( 'parts/templates/header-image' ) ); ?>

<?php Starkers_Utilities::get_template_parts( array( 'parts/templates/gallery' ) ); ?>

<?php Starkers_Utilities::get_template_parts( array( 'parts/shared/footer','parts/shared/html-footer' ) ); ?>
query_posts(['post_type' => 'galerie']);
    $terms = get_categories([
    'taxonomy' => 'galerie-kategorie',
    'hide_empty' => false,
    'orderby' => 'date',
    'order' => 'ASC'
]);
var_dump(get_queried_object());

Can someone help me please?

  • 2
    You should never use query_posts to change the what gets shown on a page, I would purge all knowledge of that function as there are no valid uses of it in normal web development. Use the pre_get_posts filter to change what gets fetched from the database instead, and you'll avoid a lot of problems, and your site will be faster. For example you might not have noticed, but your pagination will be broken because you used query_posts – Tom J Nowell Dec 4 '19 at 15:45
  • You had right! I used new WP_Query instead and everythink is fine :) – d3tr1tus Dec 4 '19 at 16:09
  • That fixes things a teeny bit, but it's nowhere near as simple/good as a pre_get_posts filter, you've gone from a 1/10 to a 3/10, pre_get_posts would be a 9/10 that uses half the code – Tom J Nowell Dec 4 '19 at 16:21
1

You don't need to use any query at all, especially not query_posts.

Instead, use the pre_get_posts action to make sure that any query for that taxonomy for a frontend archive, has the appropriate post types:


add_action( 'pre_get_posts', function( \WP_Query $q ) {
    // we only want the main query for galerie-kategorie archives
    if ( ! $q->is_main_query() || !$q->is_tax( 'galerie-kategorie' ) ) {
        return;
    }
    $q->set( 'post_type', 'galerie' );
});

Now it will make sure WP grabs what you wanted the first time around.

Otherwise, by using a whole new WP_Query/query_post you:

  • double the number of queries, WP did all that work and you've just thrown it in the trash and told it to do it all over again! Poor WP :(
  • Make your page twice as slow as it has to do everything twice
  • Breaks pagination! Now you have to do a tonne of extra work to get it working again, and you have to write custom pagination code to get the links working
  • Introduces compatibility problems with plugins and code that use pre_get_posts
  • Made your code longer
  • Cluttered up your templates, a pre_get_post filter can be hidden away in functions.php

If you ever need to modify ro change the posts WP fetches from the database, use pre_get_posts. Think of it like changing your food order before you give it to kitchen. The last thing you want is to order food, have it arrive, then change your mind and have to wait again for new food.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.