1

I am trying to exclude aside post formats from my next and previous links, but for some reason, using get_term_by is not returning me the term_id for the post_format taxonomy. I see this taxonomy being used by the Wordpress core here, so I know I'm doing this the right way. According to this posting, I need to be sure that the taxonomy is properly registered. Print debugging I see the following output from get_taxonomies().

Array
(
    [category] => category
    [post_tag] => post_tag
    [nav_menu] => nav_menu
    [link_category] => link_category
    [post_format] => post_format
    [tagportfolio] => tagportfolio
)

So I can see the post_format taxonomy has been registered. Here is my code:

<?php print_r(get_taxonomies()); ?>
<?php print_r(get_term_by('id', 'post-format-aside', 'post_format')->term_id); ?>
<?php previous_post_link('&lsaquo; %link', '%title', false, array(get_term_by('id', 'post-format-aside', 'post_format')->term_id )); ?>
<?php next_post_link('%link &rsaquo;', '%title', false, array(get_term_by('id', 'post-format-aside', 'post_format')->term_id )); ?>

Any ideas on why this isn't working? Why aren't the aside posts being excluded here?

UPDATE 1

The following code gives me back a term_id from get_term_by, but causes the next/previous links to disappear:

<?php print_r(get_taxonomies()); ?>
<?php print_r(get_term_by('slug', 'post-format-aside', 'post_format')->term_id); ?>
<?php previous_post_link('&lsaquo; %link', '%title', false, array(get_term_by('slug', 'post-format-aside', 'post_format')->term_id )); ?>
<?php next_post_link('%link &rsaquo;', '%title', false, array(get_term_by('slug', 'post-format-aside', 'post_format')->term_id )); ?>

The object returned by get_term_by is:

STDCLASS OBJECT ( [TERM_ID] => 10 [NAME] => ASIDE [SLUG] => POST-FORMAT-ASIDE [TERM_GROUP] => 0 [TERM_TAXONOMY_ID] => 10 [TAXONOMY] => POST_FORMAT [DESCRIPTION] => [PARENT] => 0 [COUNT] => 0 [FILTER] => RAW )

UPDATE 2

When I enable the Wordpress query debugging, I see the following query being triggered, which is the empty set.

mysql> SELECT P.ID FROM WP_POSTS AS P INNER JOIN WP_TERM_RELATIONSHIPS AS TR ON P.ID = TR.OBJECT_ID INNER JOIN WP_TERM_TAXONOMY TT ON TR.TERM_TAXONOMY_ID = TT.TERM_TAXONOMY_ID WHERE P.POST_DATE < '2015-06-01 20:10:00' AND P.POST_TYPE = 'PROJECT' AND TT.TAXONOMY = 'CATEGORY' AND P.ID NOT IN ( SELECT TR.OBJECT_ID FROM WP_TERM_RELATIONSHIPS TR LEFT JOIN WP_TERM_TAXONOMY TT ON (TR.TERM_TAXONOMY_ID = TT.TERM_TAXONOMY_ID) WHERE TT.TERM_ID IN (10) ) AND ( P.POST_STATUS = 'PUBLISH' OR P.POST_STATUS = 'PRIVATE' ) ORDER BY P.POST_DATE DESC ;

Empty set (0.01 sec)

What's strange about the above is that the query appears to be attempting to find the previous post based on the category taxonomy, which I have explicitly asked the function not to do through the query parameters. This seems like a bug in Wordpress to me.

  • The previous_post_link and next_post_link much take Array or a comma-separated list of numeric terms IDs for the excluded_terms i.e. the 4th parameter – Karun Jun 29 '15 at 5:05
  • Fair enough. But why does get_term_by('id', 'post-format-aside', 'post_format')->term_id return nothing in this case when there are aside posts in the database? – markshiz Jun 29 '15 at 5:07
  • get_term_by will take either 'id', 'slug', 'name', or 'term_taxonomy_id' which you already know but when your 1st paramert is id your 2nd parameter must be the taxonomy id which is probably a number. Read Here – Karun Jun 29 '15 at 5:10
  • @Karun Thanks! get_term_by now returns a term_id, but strangely, the above code causes the next and prev links to be blank. Thoughts? – markshiz Jun 29 '15 at 5:26
  • May be the get_term_by returned all ids making the pagination blank. Check the ids returned by the function. I think there is no post to be displayed next or previous. – Karun Jun 29 '15 at 5:50
0

I filed a bug against Wordpress as the query here leads me to believe that this is a bug in the framework: https://core.trac.wordpress.org/ticket/32833#ticket

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.