Forums

Include/Exclude posts by tag name ? (4 posts)

  1. valuser
    Member
    Posted 10 months ago #

    Have a function to include only posts with a certain tag id (say 21) on the home page.

    `function onlyinclude_tag($query) {
    if ( $query->is_home) {
    $query-> set('tag__in',array(21));
    }
    return $query;
    }
    add_filter('pre_get_posts','onlyinclude_tag');`

    Would prefer if I could include by tag name instead tag id !

    Any help much appreciated.

    I think i am looking for something to identify the tag id from the name (could be wrong) like say

    $tag_id = get_tag_ID('the name'); -----> made up --> but i'm looking for something like it ??

    obviously there may be a simpler way!

  2. nofearinc
    Member
    Posted 10 months ago #

    You probably need the get_term_by function - you could pass name, id or slug as a parameter and grab the ID from there.

  3. keesiemeijer
    moderator
    Posted 10 months ago #

    Not sure what you want but you could also use "tag_slug__in"

    $query-> set('tag_slug__in',array('the-name'));
  4. valuser
    Member
    Posted 10 months ago #

    Many thanks@nofearinc and @keesiemeijer. This time I have gone with @keesiemeijer and it DOES the job. I will also read up on the get_term_by function.

    I hope you know that the anwsers on this forum are just spot on and are really helpful. Thanks.

Reply

You must log in to post.

About this Topic

Tags