Advanced custom field individual value for individual post in WordPress
I am trying to show a premium tag beside post that have a 'yes' value selected in an advanced custom field named 'ispremium'. Basically there are two values in ispremium radiobutton field as 'yes' and 'no' I want when 'yes' value is selected for a post then it will show a premium tag beside post title.
I am trying this with following code but problem is that when one post is selected as 'yes' it is showing premium tag for all posts.
<?php
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'post',
'meta_key' => 'ispremium',
'meta_value' => 'yes'
));
if($posts)
{echo '<span class="scriptolution-express"> Premium </span>';}
?>
Solutions
If you only want list of premiun posts:
$posts = get_posts(
array(
'numberposts' => -1,
'post_type' => 'post',
'meta_query' => array
(
array
(
'key' => 'ispremium',
'value' => 'yes',
)
)
)
);
However if you want to list ALL the posts and show
Premium
text for only premium posts then do this:
$posts = get_posts(
array(
'numberposts' => -1,
'post_type' => 'post',
)
);
if($posts)
{
...
...
if(get_field('isPremium') == 'yes')
echo '<span class="scriptolution-express"> Premium </span>';
...
...
}
Similar questions
Advanced custom field - custom field of post object extracted only the first time
I have 2 posttypes books and stores where stores is assigned as postobject for books. bookcode and bookinfo are fields of posttype-book and storeurl is customfield of posttype-stores what i am trying to do...based on the store selected in the dropdown in books, i want to display the store and the corresponding storeurl from posttype store for each ...
Wordpress: Replace value of the custom field based on the value of another custom field
I'm writing a wordpress plug-in for my site that replaces (or adds if it is empty) a value of the custom field based on the value of another custom field under the same post. Example: I will have a list with comma divided values like that: apple,red banana,yellow kiwi,green etc. When I will run the script, it will find a post with custom_field_frui...
How to get Advanced Custom Field Value According using POST ID?
I have used Advancedcustomfields plugin to create Advanced Custom Fields. Now, want to get an Advanced Custom field value using post id. I have tried below and get solution. Now,please let me know if any other way I will get same result?
query custom post type by advanced custome field value
i used advanced custom field to make 3 radio buttons and every post should have one of the three values 1- first home 2- second home 3- offices i used this code and it's not working
How can I modify the value of an Advanced Custom Fields Post Object field via PHP?
I'm working on creating an import script to migrate data from an Excel spreadsheet that a client is providing, and converting each row into a WordPress post. So far, I've got the posts being created and all custom fields being filled in properly...except for one. One of the fields is Associated Parts. I'd like to use the Post Object for this field,...
Show advanced custom field value in custom column (wp_list_table)
I made a custom post type named address and added values to it with the plugin advanced custom fields. I would like to display those values in a custom column in the wp-list-table. So I managed to add a column to the custom post type(address) called views. With the code below. Now I wanted to fill this column (views) with the data from the advanced...