The problem probably comes from whenever a $post
has no terms set. You need to set $brand = array()
outside your IF
statement, add it just under get_the_terms()
. At that point it also wouldn’t hurt to wrap your switch
statement in a if( ! empty( $brand ) ) {
Another problem I see with this code is that you’re adding your term IDs to an array, then converting that array to a string, then switching them on an integer. So if you have a post assigned to Term 3
and Term 5
, whenever it hits your join it will convert to string: '3, 5'
and test the entire string against each case:
'3, 5' == 3
-> False
'3, 5' == 5
-> False
etc. You may just want to get the First term and test on that.
Related Posts:
- Showing random content / pictures from earlier posts in a sticky post?
- Avoid duplicate post from same Taxonomy
- Loop parent terms {display posts} AND loop child terms {display posts}
- The loop starting at a certain ID
- linking to post outside the loop
- Loop through posts by each term and exclude duplicate posts assigned to several terms
- View post with specific category id and name which I selected in the backend (drop-down option)
- Why does ‘get_the_ID’ work outside of the loop?
- Get page ID of page that is set as the posts page
- Print Current Post Index number within Loop
- Should we trust the post globals?
- How to get posts published between a date and today?
- Are post ID’s reliable?
- Get current post id in functions.php
- How set featured posts using checkbox in post edit screen?
- Get first post from wp_query
- get all posts ID from a category
- Do we still need to include a “if (have_posts())” in templates?
- WP_Query: Why is sticky post not first item in loop?
- Passing a hardcoded page/post ID into `get_post`
- MySQL Query to Retrieve Category from wp_posts
- Query *only* sticky posts
- Display all posts from specific categories on a page
- rewind_posts() – what actually the use of it, and where using is required or preferred?
- Skip post in loop and mixin later
- How to show list of posts by author and category?
- Get the current post ID as a variable in Javascript
- Is post ID number always incremental n+
- Override the default number of posts to show for a single loop?
- Exclude posts that only have the ‘Uncategorized’ category [duplicate]
- get comments and get posts in loop
- How do I reorder (pop and push) items from WP_Query?
- How to exclude latest x posts from a paginated query?
- How are terms connected with posts in database?
- Display content from a specific category
- Reverse chronology of post listing
- Why do I need to use The Loop on pages (inside page.php etc)?
- posts page – different lengths of excerpt
- WordPress Number of Posts Not Changing With posts_per_page
- how to display full post with pagination on home page
- Are all ID’s used unique?
- Count singular post views automatically
- Looping through posts per category gives same posts for each category
- $post->ID displays wrong ID
- Find most recent authors
- This AJAX Code Doesn’t Work – Looking for elegant solution
- Display All Sticky Post Before Regular Post
- Cannot access current post’s ID in custom plugin
- Can’t show comments count per post outside loop
- At my posts archive page, outside the loop, get_the_id() returns the top most post’s ID
- Importing data from spreadsheet into wordpress DB, along with custom taxonomies and their terms
- Get the author of the latest revision
- How to have different content in the loop and single
- Specific loop in Shortcode
- Strange switch_to_blog() issue
- Show a list of recently viewed posts to a user
- Multiple loops in same page, without duplicate content
- loop not displaying posts on custom template page
- Repost post on specific date every year
- How to use alternate post layout at key points in post loop?
- Alternate custom content in the loop
- Search widget breaks when using multiple loops?
- Get the post_id of a new post
- How to Display Post View Count
- get post id in while loops outputting page id
- get_terms parent for current product only
- How can I display a specific user’s first published post?
- deleting terms programmatically
- Undefined variable post_id in custom quick edit coloumn
- Adding Multiple Values to a Post Meta Key
- Displaying the number of posts in a custom loop (without including the number of posts of a loop below)?
- Display Latest Post from all Categories
- Auto Refresh Post List after X seconds
- The correct way to call posts with ajax
- custom wordpress post loop – hide iframe content
- Double count view in archive.php
- Divide Loop Into Days & Categories
- Converting multiple loops into one single loop with pagination
- Display posts with comments closed, with pagination?
- Group posts by year in loop
- How would I get 1 latest post from a query for 5 posts?
- Getting post id from wp_insert_post_data function?
- Does an article (post) id ever change?
- How to get the post’s parent ID?
- Can a page_id and a post_id be same?
- While in “the loop”, detect if a post is the most recent
- Is there a better, more efficient way to get the post id outside the loop?
- Alternating post layout
- Check to see if specific loop has less than certain amount of posts
- Different style for most recent article
- Contact form 7 post loop [closed]
- Differentiate between posts and pages in search results
- Related posts loop based on tags AND categories doesn’t work without at least one tag assigned
- Loop through all product posts?
- Getting current post ID in functions.php
- Buddypress activity id
- How to display different number of posts
- If Specific Post, Link Elsewhere
- How to filter posts that belong to a specific category only if that is the only category
- WP Query – duplicated posts once including tags in search results