WordPress.org

Ready to get started?Download WordPress

Forums

Taxonomy in loop (4 posts)

  1. Amit Kvint
    Member
    Posted 1 year ago #

    Hi,
    Can anyone please tell me if you have an idea why doesn't this work :

    <?php
    $args = array(
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'locations',
    		)
    	)
    );
    $query = new WP_Query( $args );	?>
    <?php if (have_posts()) : ?>
    	<?php while (have_posts()) : the_post(); ?>
    		<h2>
    		<a href = "<?php the_permalink(); ?>"
    		rel = "bookmark" title = "
    		<?php the_title_attribute(); ?>"
    		alt = "
    		<?php the_title_attribute(); ?>"
    		>
    		<?php the_title(); ?>
    		</a>
    		</h2>
    	<?php endwhile; ?>
    	<?php else : ?>
    	<h2>Sorry nothing Found</h2>
    	<?php endif; ?>

    I obviously have created to taxonomy in my functions.php, one of them is called 'locations' and I have a few posts connected to it, problem is the loop ignores my new WP_query and shows all of them, if I specify only one term ('locations' => 'vejer') it does work but I can't seem to be able to tell the loop to show all posts under 'location' when trying to do that I get all posts.

    Amit

  2. annedorko
    Member
    Posted 1 year ago #

    Applying a taxonomy to a post type means that all of those posts qualify as being part of that taxonomy. As someone said here, that's like looking for all posts under "Categories" or "Post tags".

    So if you've applied the Locations taxonomy to your posts, then every post technically could be considered to be part of that. I am guessing you're hoping to only select posts with active Location taxonomies applied?

    There's a solution in the link I shared above, and here's another example of how you might iterate through your custom taxonomy.

    Hopefully this helps. Let me know if you're still a little lost or whether this isn't what you were looking for!

  3. Amit Kvint
    Member
    Posted 1 year ago #

    Hey there pirate ninja,

    It's exactly(!) what I was looking for, found another (not so aesthetic approach) but I'll take a look into the example you have attached and let you know,

    Thanks

    Amit

  4. annedorko
    Member
    Posted 1 year ago #

    Sure thing! :) Good luck, and don't forget to mark this thread as resolved if you figure something out that works.

    If you're still running into issues just post back with specifics!

Topic Closed

This topic has been closed to new replies.

About this Topic