Wordpress: trying to get posts by tag

前端 未结 3 1203
忘了有多久
忘了有多久 2020-12-10 02:42

I\'ve written some code which automatically creates some posts and adds a tag to them. I can see the tags in the \'All posts\' admin panel and I can click on the posts \'Ta

3条回答
  •  北海茫月
    2020-12-10 03:41

    Answer was found here - https://codex.wordpress.org/Template_Tags/get_posts

    Following example displays posts tagged with 'jazz', under 'genre' custom taxonomy, using 'tax_query'

    $args = array(
        'tax_query' => array(
            array(
                'taxonomy' => 'genre',
                'field' => 'slug',
                'terms' => 'jazz'
            )
        )
    );
    $postslist = get_posts( $args );
    

    So for you it will be

    $args = array( 
            'posts_per_page' => 5,
            'tax_query'      => array(
                array(
                    'taxonomy'  => 'post_tag',
                    'field'     => 'slug',
                    'terms'     => sanitize_title( $brand_name )
                )
            )
        );
    
    $postslist = get_posts( $args );
    

提交回复
热议问题