We have a wordpress site using Twig and advanced custom fields to show a custom post type value on a page. There used to be just one value for the role field and we used radio buttons. We had to switch that to checkboxes because multiple roles need to be selected for some posts.
That went fine but now the for loop that we had in place doesn’t work for posts with multiple roles selected. It still displays posts which have one role selected, however. I’d appreciate any help with this as it’s written in Twig and that’s completely foreign to me! Is there a way to check for both conditions and something different for each? Or is there a way to edit every existing post to update the field so that it will always return an array?
Tried checking with ACF support but they were not familiar with using twig.
This is the .twig page.
<section class="section people">
<div class="row row-17-column">
<h1 id="team" class="header-large center">Our Team</h1>
<div class="bio-profiles">
{% for team in team %}
<bio-profile bio-id="'{{team.ID}}'" :bio-length="{{team.biography|length}}">
<img slot="photo" src="{{TimberImage(team.photo).src}}" alt="{{TimberImage(team.photo).caption}}">
<h3 slot="name" class="header-small full-width">{{team.first_name}}<br>{{team.last_name}}</h3>
<p slot="position" class="profile-position">{{team.job_title}}<br />{{team.company}}</p>
<div slot="related" class="related-stories hide-small"></div>
<p slot="quote">“{{ team.quote }}”</p>
<p slot="bio">{{ team.biography }}</p>
</bio-profile>
{% endfor %}
</div>
</div>
</section>
Here is the array that needs to be modified to pull in more than one value. They could also have a value for director or advisor along with team. there are 3 total value options.
$team = Timber::get_posts(
array(
'post_type' => 'person',
'meta_key' => 'last_name',
'orderby' => 'meta_value',
'order' => 'ASC',
'posts_per_page' => -1,
'meta_query' => [
array(
'key' => 'role',
'value' => 'team'
)
]
));
When we switched from radio to checkboxes It still displays posts which have one role selected but if there is more than one, they don't display at all.
As has been highlighted, you're doing a query to bring back the role team as opposed to all the roles you've now added. Changing your loop to the following should be sufficient, simply update the $roles array with the value of each of your roles:
$roles = array('team', 'director', 'advisor');
$team = Timber::get_posts(
array(
'post_type' => 'person',
'meta_key' => 'last_name',
'orderby' => 'meta_value',
'order' => 'ASC',
'posts_per_page' => -1,
'meta_query' => [
array(
'key' => 'role',
'value' => $roles,
'compare' => 'IN'
)
]
)
);
Related
I have a case in which I have 2 custom post types, let's say they are projects and teams. Using Advanced custom fields, I have a relationship field while creating a project and I'm assigning which teams are working on it. (Important: there is no custom field in the teams post type). Later on, I want on single-team, to list all the projects that this team was working on.
The example that Advanced custom fields has is where you have a custom field in the team, not in the project, and I need to do the opposite. (Here is how acf documentation is https://www.advancedcustomfields.com/resources/querying-relationship-fields/).
I tried doing this, but it doesn't work, it says that I don't post a correct data.
$team_id = get_the_ID();
$posts = get_posts(array(
'post_type' => 'projects',
'orderby' => 'teams',
'post__in' => $team_id,
));
Since the ACF relationship field is located in your Project custom post type in your single-team.php file where you want to list of the projects on which the team member worked on you can do the following:
$args = array(
'post_type' => 'PROJECTS CPT NAME HERE',
'post_status' => 'publish',
'posts_per_page' => 3,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'ACF RELATIONSHIP FIELD NAME HERE',
'value' => get_the_ID(), // the ID of the member
'compare' => 'LIKE',
)
)
);
I'm trying to link a custom field to a custom shortcode so the shortcode shows displays only the posts with the custom field selected to it.
This is my code below for my shortcode as you can see the key is my custom felid "flash_deal". When I enter the shortcode I just get all the perk psots and no the custom field perk posts?
add_shortcode('foundry_flash', 'shortcode_query_flash');
function shortcode_query_flash($atts, $content){
extract(shortcode_atts(array( // a few default values
'post_type' => 'perks',
'posts_per_page' => -1 ,
'meta_query' => array(
array(
'key' => 'flash_deal', // name of custom field
'value' => '"yes"', // matches exactly "red"
'compare' => 'LIKE'
)
)
), $atts));
The code you show is simply setting up variables - not running any queries at all (see extract documentation & shortcode_atts documentation).
I assume you have more code you just didn't add here, so likely the problem is the double quotes around yes that are causing issues. It's literally looking for "quoteyesquote".
As a general working example to get "Parks" Post based on custom meta, you need to use WP_Query as in the docs here.
$args = array(
'post_type' => 'parks',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'flash_deal',
'value' => 'yes',
'compare' => 'LIKE',
),
),
);
$query = new WP_Query( $args );
Then use a Nested Loop setup to loop through the results.
I have coded up a woocommerce theme and the website has a shop page. You can find out the shop page http://bit.ly/1TSMKrV
Now, I need few things to happen on this page
The products which are priced more than $50 should only display
The products must be sorted by popularity
The filters on the left side must function properly
I have tried to place the following code above the while loop
$args = array(
'post_type' => 'product',
'paged' => $page,
'posts_per_page' => 15,
'meta_key'=>'_sale_price',
'meta_query' => array(
array(
'key' => '_sale_price',
'value' => '50',
'compare' => '>',
'type' => 'NUMERIC'
)
)
);
But this code just fulfils the first condition and not the other. For time being, I have removed the code and just used the default while loop that Woo Commerce provides.
Let me know where I am going wrong.
Thanks in advance!
When there are a lot of variations and you want to edit your stock per variation, the list of variants is not logical (in the admin). When you want to edit 1 variation you have to search the whole list (250+) to find it.
I found a solution here;
Change alphabetical sorting of attributes / variations on product page in Woocommerce shop
But when I edit a bulk edit a price and save the product, only the first 41 are updated. It is because of this line;
'orderby' => 'menu_order',
This is my code;
$args = array(
'post_type'=>'product_variation',
'post_status' => array( 'private', 'publish' ),
'posts_per_page' => -1,
'meta_key' => 'attribute_pa_kleur', //rename with your attributes
'post_parent' => $post->ID,
'meta_query' => array(
array(
'key' => 'attribute_pa_kleur' //rename with your attributes
),
)
);
How can I fix this?
I use custom fields for lots of my content on both Posts and Pages, so that half of the text will be in the main post body and the other half in a custom field.
Unfortunately, when doing this only the text in the post body is searchable.
I want to have all the custom fields included in the search (just joined together as one). Preferably without installing a plugin, as all the plugins I have seen do not seem to be able to do this or they add loads of options I don't need.
I just would like one search box, that uses WordPress's default search but finds words in custom fields as well as those in the main body.
Is this possible?
you can do this by meta_query:
Try something like
$args = array(
'post_type' => 'custom_post',
'posts_per_page' => 10,
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'custom_filed1',
'value' => $_REQUEST['custom_filed1'],
'compare' => 'LIKE'
),
array(
'key' => 'custom_field2',
'value' => array( $_REQUEST['custom_field2'], $_REQUEST['custom_field2'] ),
'type' => 'numeric',
'compare' => 'BETWEEN'
)
);
$wp_query = new WP_Query( $args );
//echo $wp_query->request;
while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
//DISPLAY POST HERE
may this help you