Create a variable from a tax query in Wordpress - php

I am using the acf load_value function to select a default value for a field. I need to run a tax query and store it in a variable to use as my default value. Here is the load_value code:
function my_acf_load_field_five( $field ) {
$field['default_value'] = $VARIABLE_HERE;
$field['disabled'] = 1;
return $field;
}
The variable ($VARIABLE_HERE) needs to a taxonomy id which I need to grab from a tax query. The tax query I need is this:
private function get_latest_news() {
$args = [
'post_type' => 'news',
'tax_query' => array(
array(
'taxonomy' => 'issue',
'field' => 'slug',
'terms' => get_field('issues_controller')->slug,
)),
'posts_per_page' => 3,
]
$results = new WP_Query( $args );
}
The following query does get the result I want. The problem I'm having is how do I then store the result of that query - which should be a simple tax id such as 233 - into a variable I can pass to the load_value function?
It is the linking of the two things I'm struggling with. Although I have used them, I don't have much experience with tax queries, when googling it shows endless examples of them but nothing to show how I would create a variable with the output.

Related

query_posts with custom taxonomy

This is to create a custom RSS feed. I have a CPT called quotes with a Taxonomy called quote_category
I want all posts where quote_category = 'Dogs' which has ID = 115. This isn't working:
$postCount = 10; // The number of posts to show in the feed
$postType = 'quotes'; // post type to display in the feed
$catName = 'Dogs';
$posts = query_posts(array('post_type'=>$postType, 'quote_category'=>$catName, 'showposts' => $postCount));
Generally speaking the consensus is to avoid query_posts at all costs. The official documentation even backs this up:
Note: This function will completely override the main query and isn’t intended for use by plugins or themes. Its overly-simplistic approach to modifying the main query can be problematic and should be avoided wherever possible. In most cases, there are better, more performant options for modifying the main query such as via the ‘pre_get_posts’ action within WP_Query.
Instead, get_posts() will do everything that you want it to do. For your case, you just want to add on a tax_query with your information.
$posts = get_posts(
[
'post_type' => 'quotes',
'numberposts' => 10,
'tax_query' => [
[
'taxonomy' => 'quote_category',
'field' => 'name',
'terms' => 'Dogs',
],
],
]
)
The terms parameter also accepts an array if you need multiple:
'terms' => ['Dogs', 'Cats'],
Edit
If you are using get_template_part then you are probably also using the standard WordPress loop, too. That is actually one of the few times that you could use query_posts but I still personally just don't use it nor recommend it. In an ideal world, you'd probably use a filter just as pre_get_posts but I unfortunately I don't have time to write code to test that. You should be able to use the below code.
This code is an action that calls an anonymous function which adds a feed using another anonymous function as a callback. Your code does the exact same thing, however you are using named functions which is 100% fine, safe and common to do. I just personally prefer to keep my hooks and logic all together and not chase names down. Once again, totally personal preference.
If you still receive an internal error, make sure that both WordPress and PHP debugging is enabled so that you can see what the actual error is, and it is probably a little typo on my part.
add_action(
'init',
static function () {
add_feed(
'quotes',
static function () {
// Override the main query since the render functions wants to use the loop
global $wp_query;
$wp_query = new WP_Query(
[
'post_type' => 'quotes',
'post_count' => 10,
'tax_query' => [
[
'taxonomy' => 'quote_category',
'field' => 'name',
'terms' => 'Dogs',
],
],
]
);
get_template_part('rss', 'quotes');
}
);
}
);

How to find users having a single meta value when meta key is an array?

I'm trying to filter all users subscribed to a particular event.
All users have a meta field called 'signedto', which stores event id the user is subscribed to.
Now, I'm trying to get only users subscribed to $event_id using a meta query, but it's not working as expected (it's returning all users).
This is the code I tried:
$event_ID = get_the_ID();
$args = array(
'meta_query' => array(
'key' => 'signedto',
'value' => $event_ID, // I already tried with serialize($event_ID) and array($event_ID)
'comparison' => 'IN' // not sure about it, I also tried with 'LIKE'
)
);
$users = get_users($args);
Thanks a lot

How To Call Only Specific Tag Slugs

I'm trying to call only specific tags in my WordPress get_tags() function. Right now they're displaying ALL tags instead of just the terms in the array. Even if the tag doesn't have a post I want the tag to show which is why hide_empty => false exists. I've been toying with this along with the codex but I feel like I'm accidentally canceling out what I'm trying to do. Guidance is greatly appreciated.
<?php
$tags = get_tags(array(
'taxonomy' => 'post_tag',
'hide_empty' => false, //want to show the tags called in the terms array even if they're empty
'field' => 'slug',
'terms' => array(
'tag1',
'tag2',
),
));
Looks like your formatting is a little off. Below should return an array of tags that match your terms. The include argument requires a comma or space delimited list of ids.
You can also limit the objects it returns with the "fields" argument. See get_tags() for more info.
$tag1 = get_term_by("slug", "tag1", "post_tag");
$tag2 = get_term_by("slug", "tag2", "post_tag");
$tags_array = get_tags(array(
"hide_empty" => false,
"include" => "{$tag1->term_id},{$tag2->term_id}",
));

Woocommerce Rest API Product filters how it works?

I am trying to integrate the woocommerce rest APIs with my Applications. All the defaults operations like get all products, get products by category etc are working perfectly fine.
Can someone please let me know how are product filters implemented.?
bellow is my code.
$data = array(
'status' => 'publish',
'category' => '51',
'per_page' => 100,
'page' => 1,
'attribute' => "Color",
'attribute_term' => "Loft Gray"
);
$results = $woocommerce->get('products', $data)
Your question is really open ended. You don't have any code samples that show what you have working, such as when you say, "get products by category etc are working perfectly fine." What else do you need?
I can show you a couple examples, but who knows if it will help or not. I assume you already have a $woocommerce connection variable working...
Example 1:
$products = array();
$data = array(
'status' => 'publish',
'per_page' => 30,
'orderby' => 'date',
'order' => 'asc',
'featured' => 1
);
$products = $woocommerce->get('products', $data);//returns the first 30 featured products that are published, and sorts them by date
Example 2:
$results = array();
$data = array(
'status' => 'publish',
'category' => '51',
'per_page' => 100,
'page' => 1
//'filter[posts_per_page]' => '-1', //this was removed in v2 api
);
$results = $woocommerce->get('products', $data);//returns 100 published products of product category ID 51 (get this ID from your CMS)
//This can be used for pagination, since the filter functionality is removed
The API docs show you all the different properties you can access: http://woocommerce.github.io/woocommerce-rest-api-docs/?php#list-all-products
I hope this helps. If it doesn't, then please ask a specific question.

Why won't meta_value recognize the numeric value "1"?

I am currently working a magazine website that consists of multiple issues and various types of articles. I assigned a variable for the custom field issue number, and included it in my query in order to display only posts that are in the issue of the current page. Here's the query.
$args = array(
'post_type' => array('story','letter','interview'),
'meta_query' => array(
array(
'value' => "$issue_number",
),
),
);
$my_query = new WP_Query( $args );
This works for all issues except the first one. For some reason, my query will not recognize the numeric value "1", so I have to literally spell out "one" in the custom field for all articles that belong in the first issue.
Your meta_query is incomplete. You need to specify a key and since it's a numerical value you have to set the type. You'll also want to remove the quotes around the value.
'meta_query' => array(
array(
'key' => '_my_issue_number_key',
'value' => $issue_number,
'type' => 'NUMERIC',
),
),
Keep in mind that the issue may not be in your query but in the way you're saving the value. It would be very easy for the value '1' to be confused with 'true'. If you're still having trouble after making the changes above, open up your postmeta table and check the value being stored.

Categories