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');
}
);
}
);
Related
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.
I have a custom field that I have added to every wordpress post. I created an api endpoint that queries posts where the custom field is a certain value. That works great, the code is:
function fp_acf() {
$args = array(
'meta_key' => 'featured_post',
'meta_value' => 'yes'
);
$the_query = new WP_Query( $args );
return $the_query;
}
add_action('rest_api_init', function() {
register_rest_route('wp/v2', 'featured_posts', array(
'methods' => array('GET', 'POST'),
'callback' => function() {
return fp_acf();
},
));
});
The Problem:
The data that is returned from that endpoint doesn't contain all of the post data that is typically included in, say "/wp/v2/posts", specifically the slug or link.
Question:
I am wondering if it is possible to add the slug of the posts returned in this question query endpoint to the post data being returned?
If I understood the problem correctly and under the presumption that you are using the ACF plugin, going by the function name fp_acf, you will want to register a custom REST field that contains the value of your desired ACF field.
To get this done you have to do the following:
First, make sure all of your posts that the WP_Query is going over have the ability to be shown in the REST api.
Default posts and pages already show in the REST api but for any custom post types you will need to add 'show_in_rest' => true to their register_post_type argument array.
Next, you want to actually register the custom REST field, you do this in your rest_api_init callback like so:
add_action('rest_api_init', function() {
// ...
register_rest_field( array( 'your_post_type' ), 'is_featured_post', array(
'get_callback' => function() {
return get_field('featured_post');
},
'schema' => array(
'description' => 'Shows whether the post is featured',
'type' => 'string'
),
));
});
Obviously, you can change the return value of the get_callback function to whatever you need. You should be able to use all of the same functions, such as
get_the_permalink, get_the_title etc. as you would when looping over posts in a template.
N.B. you say you need to get the slug of the posts, you should find that there already is a slug property that is returned by the REST api for posts and pages.
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}",
));
I am developing a restful api for wordpress blog. In a url I'm returning the details of categories of that blog. The same api returns array in one blog and object in another.
The code is-
function categories(){
$categories = get_categories(array(
'orderby' => 'name',
'order' => 'ASC'
));
return $categories;
}
add_action( 'rest_api_init', function ( $server ) {
$server->register_route( 'categories', '/categories', array(
'methods' => 'GET',
'callback' => 'categories',
));
});
Here is the output-
Blog 1
Blog 2
I need same type of data to be returned, so that I can further process that data.
Advise you to please check you categories result set for both the blogs, you will be able to find the difference, In blog 1 result set something might be missing when you are preparing array for json_encode. I use to face a lot of problems like this when I started developing REST API.
If problem still exist then please post both resultsets, so that we can identify.
I can get the wordpress comments list using get_comments.
For example:
$comments = get_comments('status=approve&number=10');
It returns the trackbacks also. Is it possible to get only human comments (without trackbacks etc)? Thanks.
I believe:
$comments = get_comments('status=approve&number=10&type=comment');
Check out the documentation reference, though in the case of get_comments() it's not particularly helpful.
Also, another possible syntax which I find cleaner, is that the above syntax is equivalent to:
$args = array(
'status' => 'approve',
'number' => 10,
'type' => 'comment',
);
$comments = get_comments( $args );
The $args array defines the constraints you want when getting the comments.