• Elena

    (@elenapanzalis)


    Hi,
    I would like to display in the single page sidebar all the posts related with the current Custom Taxonomy (taxonomy name: authors) and also filter the list of posts by a tag or category.
    I found this on WordPress codex but it doesn’t work.

    Display posts tagged with bob, under people custom taxonomy:

    $args = array(
    	'post_type' => 'post',
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'people',
    			'field'    => 'slug',
    			'terms'    => 'bob',
    		),
    	),
    );
    $query = new WP_Query( $args );

    If you know some plugins or others way to do it, let me know please. šŸ˜‰

    Thank you in advance

    Elena

Viewing 10 replies - 1 through 10 (of 10 total)
  • Did you try renaming:

    'taxonomy' => 'people',

    to:

    'taxonomy' => 'authors',

    Thread Starter Elena

    (@elenapanzalis)

    yes, sure. šŸ˜‰
    Sorry I paste the code directly from the codex. In my case it would be:

    $args = array(
    	'post_type' => 'post',
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'authors',
    			'field'    => 'slug',
    			'terms'    => 'tag1',
    		),
    	),
    );
    $query = new WP_Query( $args );

    I try to explain better:
    I have a single post where the ‘author’ taxonomy value is “ElenaP”. I would like to display the posts related to “ElenaP” value from taxonomy and filter by tag ‘Tag1’.

    do you think it’s the better way to do it?
    I don’t know what to do..

    Thank you

    Try this:

    $args = array(
    	'post_type' => 'post',
    	'tag' => 'Tag1',
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'authors',
    			'field'    => 'slug',
    			'terms'    => 'ElenaP',
    		),
    	)
    );
    $query = new WP_Query( $args );
    Thread Starter Elena

    (@elenapanzalis)

    It doesn’t work. šŸ™

    But I don’t even know if it is the right way to do it.
    Maybe there are others methods to do it…?
    For example display a list of posts by current custom taxonomy and filter by category instead of tags? Or others methods?

    In theory what I posted should work, but if it doesn’t I would assume that it’s because the tag or author is incorrect.

    By the way, are the “authors” different than the wordpress users? Because if you’re looking for posts by a certain wordpress user, that’s completely different.

    Do either of these work by themselves?

    $args = array(
    	'post_type' => 'post',
    	'tag' => 'Tag1'
    );
    $query = new WP_Query( $args );
    $args = array(
    	'post_type' => 'post',
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'authors',
    			'field'    => 'slug',
    			'terms'    => 'ElenaP',
    		),
    	)
    );
    $query = new WP_Query( $args );
    Thread Starter Elena

    (@elenapanzalis)

    Ok, now it works. yeah!
    I can see a list of posts from the same taxonomy ‘ElenaP’ and filter by ‘tag1″.

    But… now you are gonne kill me…

    Do you think it is possible call the post list by CURRENT TAXONOMY?
    ‘terms’ => $current-taxonomy,

    Try this:

    $args = array(
    	'post_type' => 'post',
    	'tax_query' => array(
    		array(
    			'taxonomy' => 'authors',
    			'field'    => 'slug',
    			'terms'    => get_query_var('authors'),
    		),
    	)
    );
    $query = new WP_Query( $args );

    Give that a go, it’s untested though.

    Oh for the tag, it’d be something like:

    'tag_id' => get_query_var('tag_id')

    Thread Starter Elena

    (@elenapanzalis)

    mmm it doesn’t work.
    For the tags filter i put the slug of the tag without problems. šŸ˜‰

    I added another thing to the sidebar days ago: the description of the author in the custom taxonomy.
    Taxonomy: authors
    Name: ElenaP
    Description: Description of ElenaP

    I called it with this code.

    <?php $terms = get_the_terms( get_the_ID(), 'authors' ); ?>
    
    <?php foreach ( $terms as $term ) { ?>
    <div class="info-authors">
    <p>
    <a href="<?php echo get_term_link( $term ); ?>"><?php echo $term->name; ?></a>
    </p>
    } ?>

    With this code I can see ElenaP’s description in the sidebar, only in articles with ElenaP value.
    Do you think something like this could work?

    Thread Starter Elena

    (@elenapanzalis)

    I just tried another code like the last one I posted:

    <?php  $query = new WP_Query( 'tag=tag_slug&orderby=date' );
    
                // The Loop
                if ( $query->have_posts() ) {
                echo '<ul>';
                while ( $query->have_posts() ) {
                    $query->the_post();
    
                 $terms = get_the_terms( get_the_ID(), 'authors' );
    
                    foreach ( $terms as $term ) {
                        $term_link = get_term_link( $term, 'authors' );
                        echo '<li><a href="' . get_the_permalink() . '">' . get_the_title() . '</a></li>';
                    } ?>
    
              <?php
                }
                echo '</ul>';
                } else {
                    // no posts found
                }
            /* Restore original Post Data */
            wp_reset_postdata();

    In this case it calls all the posts with ‘tag_slug’ of ALL the taxonomy terms, not only the current. I think because the while loop.

    There’s any solution with one of these codes?
    I don’t know what to do.. šŸ™

Viewing 10 replies - 1 through 10 (of 10 total)

The topic ‘Recent Posts by Taxonomy and filter by Category or Tag’ is closed to new replies.