Elementor Pro - Custom Query - wordpress

I need some help. I am trying to create a custom query for my Custom Posts I created in Wordpress, and using Elementor Pro.
On my post, I added a custom field 'sorting' with a numeric value, which I would like to use to order my posts manually.
However, I cannot seem to get this to work.
I am using the latest Elementor pro version.
And I tried following the instructions as per their page: https://developers.elementor.com/custom-query-filter/
Here is my code I added to my theme's functions.php file
// Showing posts ordered by comment count in Posts Widget
add_action( 'elementor/query/speaker_order', function( $query ) {
// Here we set the query to fetch posts with
// ordered by comments count
$query->set( 'orderby', 'sorting' );
} );
I have added 'speaker_order' as the Query ID in the Elementor Editor.

You are close. There's one thing you left out (if I am grasping what you want to do).
It should look like this:
add_action( 'elementor/query/speaker_order', function( $query ) {
// Here we set the query to fetch posts with
// ordered by comments count
$query->set( 'meta_key', 'sorting' );
$query->set( 'orderby', 'sorting' );
} );

You have to add two more lines of code:
// Showing posts ordered by comment count in Posts Widget
add_action( 'elementor/query/speaker_order', function( $query ) {
$query->set('meta_key','sorting');
$query->set('orderby', 'sorting');
$query->set('orderby','ASC');
});

Related

Display posts on Elementor that belong to Page Author ID (Page Creator)

Hi I wanted to use elementor widgets and show some custom posts for current post author. I thought maybe with Query ID in Elemntor I can do it easily so the widget will show **some custom posts of "Page Author" I wrote a query that show the custom-post-type1 and custom-post-type2 of current post author
My query doesn't work unfortunately
function mzba_post_types( $query ) {
$query->set( 'post_type', [ 'custom-post-type1', 'custom-post-type2' ] );
$author = get_queried_object_id();
if( is_single() ){
$author = get_the_author_meta( 'ID' );
}
$query_vars[ 'author' ] = $author;
}
add_action( 'elementor/query/{$query_id}', 'mzba_post_types' );
anyone can help for a query (or any other way) that shows the Page creator custom posts in Elementor?
thank you so so mcuh for helping
This might help you. However, you can try pre-build filters as well to achieve this.
https://elementor.com/blog/introducing-advanced-query-control/

Woocommerce custom Product slug

Is there a way to create custom product URL based on the product attributes, I have a product sunglasses which has a couple of attributes tied to it: metal, blue and round, so the current URL is:
website.com/glasses/sunglasses/abram-widana-629/
What I am trying to get is a URL which has those attributes included:
website.com/glasses/sunglasses/abram-widana-meta-blue-round-629/
I would really appreciate if someone would even just point me to the right direction on how to tackle this.
There are two ways to do this, either Manually or Programmatically.
Manually adjusting permalinks:
In your example, you are simply adjusting the product URL to include attributes. This can be achieved manually by editing the permalink on the product itself.
Once the product has been added/saved, you will see the permalink showing directly under the title field like this:
Simply click the Edit button next to it and change it from abram-widana-629 to abram-widana-meta-blue-round-629
Programmatically adding attributes to permalinks:
If you want to try to achieve this permanently for all products you will have to work through the "save_post" filter/hook to add all the attributes to the permalink. The only downfall from this is that you will no longer be able to adjust your individual permalinks for your products as they will simply revert back once you click save.
Below is a code example of how to achieve that:
add_action( 'save_post', 'add_custom_attributes_to_permalink', 10, 3 );
function add_custom_attributes_to_permalink( $post_id, $post, $update ) {
//make sure we are only working with Products
if ($post->post_type != 'product' || $post->post_status == 'auto-draft') {
return;
}
//get the product
$_product = wc_get_product($post_id);
//get the "clean" permalink based on the post title
$clean_permalink = sanitize_title( $post->post_title, $post_id );
//next we get all of the attribute slugs, and separate them with a "-"
$attribute_slugs = array(); //we will be added all the attribute slugs to this array
foreach ($_product->get_attributes(); as $attribute_slug => $attribute_value) {
$attribute_slugs[] = $attribute_value;
}
$attribute_suffix = implode('-', $attribute_slugs);
//then add the attributes to the clean permalink
$full_permalink = $clean_permalink.$attribute_suffix;
// unhook the save post action to avoid a broken loop
remove_action( 'save_post', 'add_custom_attributes_to_permalink', 10, 3 );
// update the post_name (which becomes the permalink)
wp_update_post( array(
'ID' => $post_id,
'post_name' => $full_permalink
));
// re-hook the save_post action
add_action( 'save_post', 'add_custom_attributes_to_permalink', 10, 3 );
}

Excluded category posts from admin, add them to search

The story is like this, I wanted to createa new menu in WP Admin that displays only posts from a specific category (cat 13 in our case). While this works pretty well, the problem is, when I try to used the WP admin search, no posts from that category is displayed. I'm not sure how to approach this, at this moment I'm scratching my head.
Current code is:
function exclude_category_posts( $query ) {
if ( $query->is_main_query() && is_admin()) {
if($_REQUEST['page_type']=="single_cat")
$query->set( 'cat', '13' );
else
$query->set( 'cat', '-13' );
}
}
add_filter( 'pre_get_posts', 'exclude_category_posts' );
How can I do this to allow the search to look for posts from that category..or better, on that separate page I made, to make the search look only for posts from that category.
Thanks in advance!

Custom post type archive in sidebar widget of post

This works fine. I see custom post type archive in dropdown but when I select any month, I see all custom posts on that archive page. While there is only one post in every month, for e.g. I have 1 post in month of July and 1 in August, the dropdown counter shows correct but when I select them I see all custom posts instead of just one. Is there any solution for this?
add_filter( 'getarchives_where', 'custom_getarchives_where' );
function custom_getarchives_where( $where ){
$where = str_replace( "post_type = 'post'", "post_type IN ( 'post', 'fruits' )", $where );
return $where;
}
add_action( 'pre_get_posts', 'add_podcasts_to_archive' );
function add_podcasts_to_archive( $query ) {
if ( $query->is_main_query() && $query->is_archive() && !is_admin() )
$query->set( 'post_type', array( 'post', 'fruits' ) );
return $query;
}
The code in your question works. I truely suspect that your problem lies in your archive.php template.
Here are my thoughts
you have replaced the main query with a custom query. An improper custom query on any archive page is disasterous. This is the only reason I can think of given the reason that you get all post from your custom post tytpe
My solution would be, delete your custom query, replace it with the default loop, and if you need any more customization, add it with pre_get_posts.
EDIT
For extra reading that might you lots in future
When to use a custom query and when not

how to sort articles in the designated category of WordPress?

There are many categories in my wordpress ,and there are many articles in every category.
Now i want to sort articles in the 'python' category at my willing .
How can i do then?
you will have to add a meta box to save a metakey for each post as i can't see a logic for the sort order you want, something like a orderby num e.g. 'pythonsort' see - http://codex.wordpress.org/Function_Reference/add_meta_box
then you can add this to your functions file:
function sort_by_order(){
if ( is_admin() ) {
$wp_query->set( 'orderby', 'meta_value_num' );
$wp_query->set( 'meta_key', 'pythonsort' );
}
}
add_filter('pre_get_posts', 'sort_by_order' );

Resources