WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. valuser
    Member
    Posted 1 year 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 1 year 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 1 year 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 1 year 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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags