i would like to sort my posts first by "time" meta value and secondly by "clicks" meta value.So if 2 posts have the same "time" the one with less clicks will go first.
$args=array(
'post_type' => 'post',
'category_name' => 'players',
'order' => 'ASC',
'meta_key' => 'times',
'posts_per_page'=> '15',
'orderby' => 'meta_value_num',
'offset' => $offsetnumber
);
You can hook into the posts_orderby filter to customize the sql query to sort by multiple fields.
Some details here:
http://jeffgran.com/262/blog/wordpress-sort-posts-by-multiple-fields-part-ii
Related
I have set on my WordPress post custom meta called "my_sort" which have a numeric values like 1, 2, 3, 4..... See below image:
I want to sort in ascending order by this meta "my_sort". This is the below code I am applying:
<?php
$catPost = get_posts(array('category' => get_cat_ID($categories[0]->name), 'meta_key' => 'my_sort', 'orderby' => 'meta_value_num', 'order' => 'ASC', 'numberposts' => 100)); //change this
?>
The problem with this code is that it is working but has one problem which I need to be fixed.
It is leaving all the other posts which do not have "my_sort" meta in them. I want to include those posts also. I want:
First the posts that have "my_sort" must come in ascending order.
Then the post that do not have "my_sort" meta should come.
Update
When I try the below query.
$catPost = get_posts(
array (
'category' => get_cat_ID($categories[0]->name),
'numberposts' => 100,
'orderby' => 'meta_value_num',
'meta_type' => 'NUMERIC',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key'=>'my_sort',
'compare' => 'EXISTS'
),
array(
'key'=>'my_sort',
'compare' => 'NOT EXISTS'
)
),
)
);
I do not get proper result. See the image which shows the result.
Here:
Record1 : does not have "my_sort" meta.
Record2 : does have "my_sort" and it's value is 2.
Record3 : does not have "my_sort" meta.
Record4 : does have "my_sort" meta and it's value is 4.
The result in this case should be like this:
Record2 then Record4 then Record1 then Record4 but clearly this is not the case.
What is wrong?
If you want to sort by a meta key that might not have any values, you need to use a meta_query that combines the results of a search for posts with the key and a search for posts without the key.
This isn't tested, but the main logic is there:
$catPost = get_posts(
array (
'category' => get_cat_ID($categories[0]->name),
'numberposts' => 100,
'orderby' => 'meta_value_num',
'meta_type' => 'NUMERIC',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key'=>'my_sort',
'compare' => 'EXISTS'
),
array(
'key'=>'my_sort',
'compare' => 'NOT EXISTS'
)
),
)
);
I have built a search a form at custom page in wordpress and want to filter product on shop page using meta keys that are already exists into posts table.
Initially I have tried to filter categories at form page like this but it doesn't work.
$meta_query = array(
'key' => '_years',
'value' => '2009'
);
$args=array(
'meta_query' => $meta_query,
'posts_per_page' => 10,
'post_type' => 'product',
'orderby' => $orderby,
'order' => $order,
'paged' => $paged
);
wc_product_dropdown_categories($args);
The meta_query parameter must be change in an array of array for a single custom field handling:
$meta_query = array(
array(
'key' => '_year',
'value' => '2009',
'compare' => '>',
)
);
Some details from WP_Query page in the Single Custom field handling part
Hope it will work with this.
I'm trying to pull out all of the posts that have a meta value of Main, while trying to ignore the newest post created. I've looked here (WordPress site https://codex.wordpress.org/Class_Reference/WP_Query) but I can't figure out a way to ignore the newest post. It can't be by a set date, because it needs to always ignore the newest post.
$args = array(
'order' => 'DESC',
'meta_key' => 'main_story',
'meta_value' => 'Main',
'meta_query' => array(
'relation' => 'NOT IN',
array(
'key' => 'main_story',
'value' => Main,
'posts_per_page' => 1,
'order' => 'DESC',
),
)
);
I thought trying it this way not not get the newest post, but it hasn't worked. Is there an opposite of getting the meta_query, like ignore_meta_query, but you know, that works?
There are a lot of issues with your posted code...but you're looking for the offset parameter of WP_Query.
offset (int) - number of post to displace or pass over. Warning: Setting the offset parameter overrides/ignores the paged parameter and breaks pagination. The offset parameter is ignored when 'posts_per_page'=> -1 is used.
$args = array(
'offset' => 1,
'posts_per_page' => 1,
'meta_key' => 'main_story',
'meta_value' => 'Main',
'meta_compare' => 'NOT',
);
When there are a lot of variations and you want to edit your stock per variation, the list of variants is not logical (in the admin). When you want to edit 1 variation you have to search the whole list (250+) to find it.
I found a solution here;
Change alphabetical sorting of attributes / variations on product page in Woocommerce shop
But when I edit a bulk edit a price and save the product, only the first 41 are updated. It is because of this line;
'orderby' => 'menu_order',
This is my code;
$args = array(
'post_type'=>'product_variation',
'post_status' => array( 'private', 'publish' ),
'posts_per_page' => -1,
'meta_key' => 'attribute_pa_kleur', //rename with your attributes
'post_parent' => $post->ID,
'meta_query' => array(
array(
'key' => 'attribute_pa_kleur' //rename with your attributes
),
)
);
How can I fix this?
I have a template that pulls a custom post type for properties and sorts them by state. The script works properly until you add in a meta_query for the agent. It pulls the correct properties, but it doesn't sort them by state with the meta_query present. Here's the query:
$qry = array(
'post_type' => array( 'practices-tpsg' ),
'meta_key' => 'wpcf-practice-state',
'orderby' => 'meta_value',
'order' => 'asc',
'showposts' => 18,
'paged' =>$paged
);
if($_GET['agent'])
{
$qry['meta_query'] = array(
'relation' => 'OR',
array(
'key' => 'wpcf-agent',
'value' => $_GET['agent']
),
array(
'key' => 'wpcf-agent2',
'value' => $_GET['agent']
),
);
}
Does anyone know why it would stop sorting properly after the meta_query is added to the query?
Here's a link to the page: http://www.totalpracticesolutionsgroup.com/practices-for-sale/ The sort order works properly in the default view, which allows the state name headers above the listings. If you select an agent from the drop down in the top right, the correct properties are pulled, but they are no longer sorted by state. You can also see the output of the query by adding &debug=1 to the end of the url.
replace 'orderby' => 'meta_value' with 'orderby' => 'meta_value_num