I don’t think it’s just occasionally, that will always get you the post meta of the first post on a search results page, a taxonomy page, an archive page – any page where there are multiple posts, because the $post
global will always be populated with the first post of any main query result.
EDIT-
if ( is_singular() ) :
// we are viewing a single post or page
$page_color = get_post_meta($post->ID, 'page_color', true);
else :
// not a single post or page, use a default color
$page_color="blue";
endif;
Related Posts:
- have_posts() execution failure
- Get all posts as an array ID => Name
- Query multiple custom post types in single loop
- Inject post (from specific category) between posts in Loop
- Build a content and excerpt grid loop with paging and options for # of posts
- How to create a WP_Query to search the Title or Tag?
- Guidance with The Loop for CMS
- Alter secondary loop to exclude posts from current page category
- Adjust the results quantity for Search Results page pagination
- How can I loop into two different DIVS without repeating the DIVs
- Search widget breaks when using multiple loops?
- WordPress loop specific thumbnail size
- How do I dynamically generate args for wp_query?
- How to get to a specific position in wp_query?
- Use wp_get_recent_posts with search term
- How to do multiple searches (with logical OR) in WP_Query in hook pre_get_posts?
- Can’t search posts using WP_QUERY inside AJAX Function
- WordPress pagination not working with search page
- Using foreach loop breaks
- WordPress Search Results for Multiple Post Types
- WP Query with custom Shortcode
- Recent Posts Not Showing Only On A Specific Category Page [closed]
- How to make search for posts using get method?
- How Can I use WP_Query to Only Display 1 Post from Custom Post Type if Query Returns Posts with Matching ID in Custom Field
- Syntax error when I try to insert my loop into an unordered list? [closed]
- WP_Query fails despite having 1 post
- wp_query get the 2nd post
- Exclude posts with specific metadata from search?
- How to get post ID in a Page?
- Display an image if odd number of posts in grid
- Search form does not work in my website
- How to add div blocks after certain set of post
- How to overwrite orderby with add_query_var
- What file have I to create in my custom WordPress theme to show all the post belonging to a specific category?
- Why in my theme I can’t see all the statics content under the posts?
- get_the_ID() in the footer returns wrong value
- Form search query – displaying ACF “Post Object” field as results
- Getting posts to exclude from array
- home.php show blog posts as grid view
- Custom post type permalinks do not appear using the link functions
- WP_Query multiple post results
- Do not duplicate posts with multiple categories in multiple loops
- Infinite Loop – WP_Query
- Dividing the loop to style post differently
- Search only working on front page (index) , not working on other pages
- How to extract information from a wp_query result?
- Search page results conflicting logic – Search result caching?
- Toolbar Hidden in a Virtual Page
- the_post(); prints out style text into my HTML?
- Search AJAX Filters – Multiple Query Loops Into One Loop (Optimization)
- Wp Query : Order by distance lat,lon
- How can I modify this code to make the search box include tags and meta
- Free search and custom taxonomy query not providing the same result
- search.php to search only the post title
- WordPress Search Ajax + Isotope + InfiniteScroll
- Replace a single variable with add_filter
- Using a new WP_Query inside the loop
- in_array not working on dev server but works on localhost
- meta_query search names when they have middle initials
- Check if current post in loop is last on current page
- Looping through dummy posts and showing them in the main index.php
- Trying to get pagination working on WP_Query() post grid
- How to display sticky post always at the top (before regular post) in wordpress?
- Making list of posts with chosen description
- Include search tags and users in my search results system
- what’s the syntax for if the image exists get the image?
- making a search.php query
- Problems with the WordPress loop [closed]
- My query keeps looping infinitely ! how to stop it?
- Efficient way of querying for a “fallback” post?
- How to add thumbnails from recent posts to owl-carousel in wordpress automatically?
- For each 3 posts, show a different post type
- Add post class in custom loop (WP_QUERY)
- Create a hierarchical loop at predefined markup requirements
- WP Knowledge Base Theme bug – Subcategories and Articles, Need to change WP_Query
- Advanced Taxonomy query, with 3 post types
- Trying to display ads on only a third of posts in a loop. Keep getting Error 500?
- Why does a meta_query break this WP_Query?
- WordPress post pagination on custom template not working
- Pagination in category.php not functioning
- Pagination not working properly
- $_html is empty when var dumped
- Variable ++ in query loop
- How to use a conditional statement in a post loop but not count towards the “posts_per_page” if false
- How do I exclude the current post from the upcoming post query
- How do I display WooCommerce products in my query to rows of 3?
- How to get posts by a certain author in inner loop using outer loop variable or post title WordPress
- WordPress Themes and PHP unit
- Way to display “Yesterday”, “Today”
- Using a `Template Parts` folder instead of an `Includes` folder in a Custom WordPress Theme
- How to disable controls in theme customizer?
- WordPress import media error
- Show recent posts starting at a specific number archive
- security concerns if using html data-* attribute for l10n?
- How do if all posts has this category ID then do this
- View tag description on page
- Display product attributes for current product
- Masonry layout within the loop
- The normal loop with different styles doesn’t work in search.php
- is there away to hide php code in wordpress not to show in PAGES