I am trying to display posts using the REST API and I want to display them in menu order based on how they are positioned in the admin menu.
I installed a plugin that allowed me to move posts to any position in the admin. I know querying the posts the normal way with wordpress there is a orderby: menu_order option, which would do what I am looking for, but I can't figure it out with the REST API.
My REST API looks like this:
https://example.com/wp-json/wp/v2/qd_leadership?_embed&per_page=100&orderby=menu_order
So I've tried that and that does not work. It says menu_order isn't an option. I also saw a post here:
Query WordPress (REST) posts by the order they appear on the admin
That had a similar question. The only answer on that post is to not have any orderby parameter and that should display them in the menu order, but that did not work for me. So I am stumped on how to order the posts from the REST API in menu order?
Its bug of wp core in rest api so you could use below hack for the solution.Please add below code in your active theme's function.php
add_filter( 'rest_post_collection_params', 'my_prefix_add_rest_orderby_params', 10, 1 );
function my_prefix_add_rest_orderby_params( $params ) {
$params['orderby']['enum'][] = 'menu_order';
return $params;
}
Tested and works.
Thanks to raju_eww for the hint in the right direction. But in case of a custom post type collection the filter hook name has to be like this:
add_filter( 'rest_custom-post-type_collection_params', 'my_prefix_add_rest_orderby_params', 10, 1 );
function my_prefix_add_rest_orderby_params( $params ) {
$params['orderby']['enum'][] = 'menu_order';
return $params;
}
found here:
https://www.timrosswebdevelopment.com/wordpress-rest-api-post-order/
Following code do sort by menu_order when orderby is not in query string (for woocommerce):
add_filter('woocommerce_get_catalog_ordering_args', 'am_woocommerce_catalog_orderby');
function am_woocommerce_catalog_orderby( $args ) {
if(!$_GET['orderby']) {
$args['orderby'] = 'menu_order';
$args['order'] = 'asc';
}
return $args;
}
source
Related
really hoping someone can help with this as I thought it would be far more simple!
Long story short, I have created a script that populates the upsells of WooCommerce products, all works great using the API and they are there. They show on the product page as expected but in a completely different order to how they were inputted in the admin area and I cannot seem to find a way for the order to follow admin?
function filter_woocommerce_upsells_orderby( $orderby ) {
return 'menu_order';
};
add_filter( 'woocommerce_upsells_orderby', 'filter_woocommerce_upsells_orderby', 10, 1 );
Above is the hook I have found but from the options I have found such as menu order / id / price etc, there is not simply an overide option to ignore the order and just take them as they are in admin!?
Please help!
I also encountered this problem. And i have an idea.
I checked all wordpress parameters about Order & Orderby.Link is https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters.
And i use the paramteters name "none",It can make your orderby not based on any sorting rules.In other words, it is sorted according to upsells. here is the code.
// ORDER BY
add_filter( 'woocommerce_upsells_orderby', 'filter_woocommerce_upsells_orderby', 10, 1 );
function filter_woocommerce_upsells_orderby( $orderby ){
return "none";
};
// ORDER
add_filter( 'woocommerce_upsells_order', 'filter_woocommerce_upsells_order', 10, 1 );
function filter_woocommerce_upsells_order(){
return 'asc'; // Default is 'desc';
};
But it is still chaotic, when adding any product to upsells, it is still random. Therefore I also used a plug-in "WooCommerce Drop/Drag For Upsells Cross-Sells", which allows you to drag your products in upsell at will.
If you have any question you can ask me.
Thanks.
I need help with WordPress. For example, I have 100 posts in my blog, and only one post has a word: "HOT". I'd like to show this post on the right side (like widget). Can you advise me something?
There are plugins like "Recent Posts Widget Extended" that can filtered by tag or filter the post query on functions.php like :
add_filter( 'rpwe_default_query_arguments', 'your_custom_function' );
function your_custom_function( $args ) {
$args['s'] = 'HOT';
return $args;
}
I've got the same problem described in this post:
[WordPress URL rewrite for WooCommerce attributes, except that I need to filter by attribute not only inside a category.
Unfortunately, I cannot post a comment before reaching a higher reputation, so I'm creating a new question.
I defined a manufacturer attribute and if I want to browse all products from a certain manufacturer, I can use a url like www.example.com/shop/?filter_manufacturer=230, where 230 is the attribute ID.
I tried adding a endpoint, like suggested in the post linked above above, but I cannot get the rewrites working; for example, if I try to open www.example.com/shop/manufacturer/manufacturer_name I get a 404 error.
It's not clear to me if I should change anything in the permalink settings in Wordpress and, if yes, how.
I've always flushed the rewrite rules after every edit, BTW.
The missing link between your question regarding WooCommerce attributes and the linked answer is that product attributes are merely taxonomies with a 'pa_' appended to their name.
In your case the taxonomy is called "pa_manufacturer". WooCommerce sets these up by default to have no query var attached.
So in lieu of filtering query_vars we are going to target when WooCommerce registers that particular taxonomy. I've also modified to remove anonymous functions.
In my example I am using "color", so adjust to "manufacturer". I was able to then go to a URL of http://example.com/shop/color/black and see all the black products. Note that this doesn't get you a term archive where /shop/color will list all the colors. That is a different question and a lot more work.
I didn't test the activation part, so if you get 404s after activating you can just delete the whole activation function and simply go to Settings>Permalinks and save the permalinks again.
/**
* Plugin Name: Add an WooCommerce attribute endpoint to the URLs
* Plugin URI: http://stackoverflow.com/q/28460538/383847
* Credit to: http://stackoverflow.com/a/24331768/1287812
*/
function so_28460538_add_rewrite_endpoint(){
add_rewrite_endpoint( 'color', EP_ALL );
}
add_action( 'init', 'so_28460538_add_rewrite_endpoint' );
function so_28460538_attribute_args( $args ){
$args['query_var'] = 'color';
return $args;
}
add_filter( 'woocommerce_taxonomy_args_pa_color', 'so_28460538_attribute_args' );
/**
* Refresh permalinks on plugin activation
* Source: http://wordpress.stackexchange.com/a/108517/12615
*/
function WCM_Setup_Demo_on_activation(){
if ( ! current_user_can( 'activate_plugins' ) )
return;
$plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : '';
check_admin_referer( "activate-plugin_{$plugin}" );
add_rewrite_endpoint( 'color', EP_ALL ); #source: http://wordpress.stackexchange.com/a/118694/12615
flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'WCM_Setup_Demo_on_activation' );
EDIT
Adding some screenshots of my settings in case it will help determine why you are getting 404s:
Here are my permalinks settings:
and here is the WooCommerce setting for determining the product archive page:
And finally, here is the result of visting:
http://local.wordpress.dev/shop/color/black/
Where shop is the pretty permalink for the product archive page set above. All items have a 'black' color attribute.
I had a similar problem and was able to solve it like this:
function add_model_taxonomy_args($args) {
$args['query_var'] = 'filter_model';
return $args;
}
add_filter('woocommerce_taxonomy_args_pa_model', 'add_model_taxonomy_args' );
function custom_rewrite_rules() {
add_rewrite_tag('%filter_model%', '([a-zA-Z0-9-]+)');
add_rewrite_rule('^c/phone-cases/(.+?)/?$', 'index.php?product_cat=phone-cases&filter_model=$matches[1]', 'top');
}
add_action('init', 'custom_rewrite_rules', 10, 2);
Sample Url: c/phone-cases/iphone-8-plus/
Resulting Rewrite: index.php?product_cat=phone-cases&filter_model=iphone-8-plus
The response for all posts in wp-json does not include a couple of Custom Fields that I created in my posts. I call it this way: /wp-json/posts
How can I make sure the JSON response also contains my Post Custom Fields?
Check out this link.
Turns out this is a problem at the mo in Wordpress, but the link has a suggestion fix (amongs others) as such :
function json_api_prepare_post( $post_response, $post, $context ) {
$field = get_field( "field_name", $post['ID'] );
$post_response['field_name'] = $field;
return $post_response;
}
add_filter( 'json_prepare_post', 'json_api_prepare_post', 10, 3 );
You will however have to go through the full post as the above link, s it turns out a lot of people are having this issue, but a number of them got the issue resolved with all the suggestions in the post.
It also appears this question has been asked before. refer to this question.
I know that you can generate a feed using urls like: ?cat=3&feed=rss2
And you can switch it around to exclude the category 3 by putting a subtract sign in front: ?cat=-3&feed=rss2
But, it doesn't look like you can do the same for posts? I'm using the JW video Player and have loaded the related plugin. The related plugin can take an rss-feed (media rss) as the parameter so it can link to other videos/wordpress posts that are related.
My problem is that currently this means that the active video also appears in the related videos feed.
What would be the best solution for solving this problem? I aim to create my own rss feed generator in the future, but for now I just want to keep it simple and use the generated feeds that wordpress creates. Is there a simple way to add support for an url parameter named post for example? It could then take post=-7 to exclude post with id 7 from displaying in the feed.
Or is there better solutions for this?
You can use a function
function exclude_category($query){
if ( $query->is_home || $query->is_feed || $query->is_archive ) {
$query->set('cat', '-1');
}
return $query;
}
add_filter('pre_get_posts', 'exclude_category');
see'a
Instead of explaining the mechanisem - there are many plugins just for that ..
One that I know is :
StelthPubish
Edit I
I do not know about the URL - but you can try use the
function ok9_feed_filter($query) {
if ( !$query->is_admin && $query->is_feed) {
$query->set('post__not_in', array(15, 6) ); // page /post id
}
return $query;
}
add_filter( 'pre_get_posts', 'ok9_exclude_filter' );
or this
function ok9_feed_exlude($where, $wp_query = NULL) {
global $wpdb;
if ( !$wp_query )
global $wp_query;
if ($wp_query->is_feed) {
// exclude post id
$where .= " AND $wpdb->posts.ID NOT IN (15, 6)";
}
return $where;
}
add_filter( 'posts_where','ok9_feed_exlude', 1, 2 );
If you do not want to use a fixed id in the function - you can always add a custom field in the posts you want to exclude , and use that in the query ..