WP Advanced custom field value in query posts array - wordpress

I need to put an advanced custom field value (category number) from page to query_posts array. Is it somehow possible?
On a single page I'm trying to show posts from category, which number is value of this custom field.
Ideal and of course non-functional case:
query_posts(array('category__and'=>array( the_field("category"); ,99), 'posts_per_page'=>6));

the_field() displays a meta value. You'll want to use get_field() (which returns a meta value) instead:
query_posts(array('category__and'=>array( get_field("category"); ,99), 'posts_per_page'=>6));
In somewhat unrelated news, you should really consider avoiding the use of query_posts.

Related

Adding custom display_type to WooCommerce categories (product_cat taxonomy)

Under a category, WooCommerce ships with a field called 'display_type'. It has 4 values:
Default
Products
Subcategories
Both
How do I add more options in there?
I found the function in the source code: add_category_fields and it doesn't look like that there is a hook, where additional fields can be added.
Had I been lazy, then I would probably just have added a new meta-field with my customly added options (using ACF).
Bonus question: For some reason, then I can't get the value of the display_type-field, unless using $wpdb. $display_type = get_term_meta( $term->ID, 'display_type' ); Doesn't seem to work. I elaborated on this question over here.

How to get term meta WPGraphQL/WooGraphQL?

I'm using Woo GraphQL and I'm wondering how I should go about getting the color of product attributes. See below for a screenshot of the product attribute in WP Admin:
I've tried querying for the top-level pa- attribute and I've tried querying in terms/termNode with no luck.
Am I missing something? How do I get this data?
Update: So TIL, that colors in products attributes were actually provided via "Variation Swatches for WooCommerce". Variation Swatches takes this info and saves it in term meta.
So now my question is a bit different: How do I pull term meta?
The best way that I have found is to not use (or with addition to ) WooSwatches plugin us should use advanced custom fields and WPGraphQL for Advanced Custom Fields in the following way
add a new field groupe with name Color Hex that has the location rule Taxonomy is equal to Color (or what ever attribute you want to use)
add a field with the name hex (for example) with type of color picker and make show in graphql
you should be able to query the data like this
paColors {
nodes {
id
name
description
colorHex {
hex
}
}
}
You can get term meta using the get_term_meta() function. See more here.
I believe the meta key you want for this swatch_id.

ACF meta key field displays Array when field is of "relationship" type in post grid plugin wordpress

In wordpress I have a custom post type (generated through the ACF plugin) of job offers which include several text fields (meta_key values) which the plugin "post grid pro" will display no problem. However, I have one field which is the agency that posted the job offer.
This is a relationship field and whenever I add this meta_key to the list of fields to display within each block of my grid it displays simply "Array".
I have tried creating my own shortcode to display this field but alas, this does not work.
There is a page in the site, written by one of the original developers, which lists the job offers. They have managed to display the title of the job agency in php with:
<?= get_field("agence")[0]->post_title ?>
I would like to be able to either write my own shortcode to display the agency title or somehow dynamically create a new meta_key for each job offer which contains automatically the name of the agency derived from the relationship field "agence".
Any help would be vastly appreciated.
Instead of using the relationalship ACF field type, use the one called "Post Object". As long as you don't enable "Select multiple values?" option this will only return a single post object.
This post lists the difference between the two field types. Basically, they both return post objects, but they have slightly different options, and relationship fields always returns the result in an array. You could technically use the relationship field and set the minimum and maximum number of posts to 1, but the post object field is the most appropriate one to use when you only want a single post.

Advanced Custom Fields - Filtering a Post Object Field by previously chosen Taxonomy

I have a custom post type called Event in which I want to add a location chosen from a large list of locations. To make this process easier I want to filter these locations by a category called locality.
To achieve this I have created a custom post type called Locations and a custom taxonomy called Locality. A field group holding two dropdowns one for Locality (Taxonomy) and another for Locations (Post Object) has been added to each event.
What I haven't managed to achieve is the filter of locations by the locality chosen.
I don't believe you can dynamically filter the choices of one custom field based on the current choice of another, and change that choice on the adminend. You can filter the choices that show up in your fields with a query modification that's very similar to WP_Query, and I'm pretty sure that can't be done on the fly.
I would almost cheat and instead of making posts with taxonomy assigned to them, include both the locality and the location in the post name itself.
So say, post [Museum of London] with taxonomy [London]
would become
[London][Museum of London]
[London][Hyde Park]... etc
or something similar. I'm presuming you only want to assign one location to any specific locality...
Then I'd create a relationship field and whenever I'm looking for Museum of London I'd type
London Muse...
It looks like this question is stale by now, but you would make your life easier if you added two custom fields to Event - location and locality. That way you don't have to join two tables and can easily use WP meta_query to filter posts.
What i understood is that you want that whenever you select the locality the location for it should be shown there and at last you can filter Events with locations.
=> To set location you assigned two drop-down for locality and location. You should use ajax. when i select any locality than the next drop-down has the options for the selected locality.
=> To add value in event post: you might have two options
i) you may add location as taxonomy and search using "tax query" to filter
ii) add location as parent of event and search "child of" in wp_query to filter
You have two options:
Program custom js to filter the locations field onchange of the localities field. This would not be so hard to do, because ACF sets the field id's for every field in their html. You can enqueue this js script in an admin hook (probably admin_enqueue_scripts, check this solution) and filter for the custom post type.
It can all be done in the ACF gui, but it's a bit of a trick, also because of the fact that the conditional logic for acf's taxonomy field is lacking. What you have to do is create as many Location fields in the Event acf fields as you have localities. Then set these locations to specific localities. Now comes the weird part. You would want to set the conditional logic to display if the locality is e.g. Russia, but it lacks a 'specific value' field. So you have to set two rules, one to set the 'greater than' and one to set the 'lesser than' for the id of the locality. . This will be a pain if you have a lot of localities, but it might be a good option if your localities list has no more than a few items. If you name the location fields all 'location' than you can just get the value with get_field('location'). I tested this and there is no conflict in naming all the location fields location. ACF has done a pretty decent job there, having unique names for these fields also to be able to name these fields as you whish.

Extra options for Custom taxonomies Wordpress

I have to add simple options to my custom taxonomy (genre) for each custom post included in that specific taxonomy.
Taxonomy name: Genre
Taxonomies: Blues, Jazz, Rock
Custom post name: Bands
Bands: Band One, Band Two Band Three, Band Four
So basically going to the Jazz taxonomy options, should give me options for the Jazz bands:
Band Two {text_field}
Band Four {text_field}
I'm not achieving this with Advanced Custom Fields so I guess I need to write it? Or is there a plugin to save my life? If not where should I start looking?
If what you mean is that you want to extend your custom taxonomy to have more fields that the default: Name, Slug and Description, you can:
Use a massive plugin like Pods, which allows you to do what you want to do and much more stuff. See their docs.
Use a simple plugin like Tax-Meta-class, intended just for that purpose, which does not add many (probably unnecesary) code.
Try to do it manually, like in this example.
Note that I have not used any of these, but I'd strongly suggest you to try the option 2.
EDIT
Actually, if you're already using Advanced Custom Fields plugin, it allows you to do what you're looking for!
You just need to create a new Field Group and set up the custom fields you want as usual. Then, in the box Location, you need to set a Rule like this:
Show this field group if Taxonomy Term ----- equals to -----
Genre
This way you have all the power of ACF to create select-boxes or whatever you need...
EDIT 2
If what you want is to have different custom fields in a Post, depending on the Terms that this post has for a given taxonomy, you have to set up a Rule for your Field Group like this:
Show this field group if Post Taxonomy ----- equals to -----
Jazz
This will display this Field Group in all the Bands with a value of Jazz in the Genre taxonomy...

Resources