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.
Related
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');
}
);
}
);
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 used yii blog from Yii Framework
I want Show Only Published Post And Approved Comment at RecentComments List
I used This code at Comment Model
public function findRecentComments($limit=10)
{
return $this->with('post')->findAll(array(
'condition'=>'t.status='.self::STATUS_APPROVED.'status='.Post::STATUS_PUBLISHED,
'order'=>'t.create_time DESC',
'limit'=>$limit,
));
}
But Show All Post And Approved Comment in RecentComments List
I want Show Published Post And Approved Comment at RecentComments List
Setting condition for relation model is done within with() function.
I think findAllByAttributes() is more readible so here's solution using this function
public function findRecentComments($limit=10)
{
return $this->with(array('post' => array(
'condition' => 'post.status=:status',
'params' => array(':status' => Post::STATUS_PUBLISHED),
)))->findAllByAttributes( array(
self::getTableAlias(false, false).'.status' => self::STATUS_APPROVED
),
array(
'order'=> self::getTableAlias(false, false).'.create_time',
'limit' => $limit
) );
}
I am using the Posts to Post plugin for Wordpress (version: 1.6.3) to link my posts with a custom post type I created. This custom post type consists of 'authors' that can be linked to a post. Yesterday it was functioning as it should but as for today, the overview is generating a list of 'posts' instead of a list of 'authors'.
I am using the following function to generate a list of authors.
function my_connection_types() {
p2p_register_connection_type( array(
'name' => 'posts_to_auteurs',
'from' => 'post',
'to' => 'auteur'
) );
}
add_action( 'p2p_init', 'my_connection_types' );
Does anyone know what's wrong? I'm dying for help...
I am developing this yii application and in the index i am displaying a "preview" of these items and currently it is display 8 items then it will pageinate itself. I would like make it page every 4 items and i have searched online and found that i could use the CPagination. I have followed the example in the documentation http://www.yiiframework.com/doc/api/1.1/CPagination and it is working but i don't know how to display the model i have the following code in the index.
foreach($models as $model):
endforeach;
$this->widget('CLinkPager', array(
'pages' => $pages,
));
Also the data that i want to display is Title, Content, Image and id and can i use the _view since i have everying set from there regarding css.
Also by defult the yii application display the index with a CListView is there a way where i can set the item limit there instead of using CPagination
If you want to just display 4 items on the index page, edit the index action like this:
public function actionIndex()
{
$dataProvider=new CActiveDataProvider('{Model}',
array(
'criteria' => array(
'select' => 'title_field,content_field,image_field',
'limit' => {Limit},
)
));
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}
This code will return {Limit} models from the database, if you want to sort them by any field you will need to add 'order' to the criteria array lik this:
'criteria' => array(
'select' => 'title_field,content_field,image_field',
'order' => 'download_id DESC',
'limit' => {Limit},
)
In your view, use foreach and render partial to render the _view.
foreach($models as $model):
$this->renderPartial('//{Model}/_view', $model);
endforeach;
Hope that helps.