Orderby Taxonomy
-
Can ALM order by a taxonomy?
Use case: I have product listings of items for sale. One of my Taxonomies is product condition (new or used). I want to be able to have my “new” condition product listings always at the top of the listings.
- This topic was modified 3 years, 11 months ago by bobhunter. Reason: Use "sort" instead of "order"
-
@bobhunter From my understanding, no you cannot order by taxonomy. This would normally be handled by a custom field.
What you would have to do is run two separate custom queries and pass an array of Post IDs to the post__in shortcode parameter.
Hope this gets you started.
So if Condition was a meta field ALM would be able to sort on it correct?
I am thinking since the database of 1,600 products already exists, it might be easiest to create a meta field that contains the taxonomy value also (yeah bad data management, but launch is next week and we are about out of time. lol.)
Yes, then you could sort by meta_key.
[ajax_load_more meta_key="fieldname" orderby="meta_value"]
Cheers
1) thank you very much @dcooney . Not only for the support but for the fantastic plugin. It’s ability to search on multiple taxonomies with different operators and all leveraged via a shortcode is AWESOME! I was blown away by how much I got out-of-the-box with ALM.
2) “What you would have to do is run two separate custom queries and pass an array of Post IDs to the post__in” – So one query to grab all the relevant post and the 2nd to sort them? I have not messed with WP custom queries, but I am comfortable with SQL and Php. I may be confused on what ALM will be doing and what my customer query will being as well as order of operations (my custom query first or ALM first then querying those results)?
@bobhunter
#1 – thank you ??
#2 – It’s hard to explain over these forums but essentially the following:Query #1
– Get all ‘NEW’ items
– Append Post ID’s to the array.Query #2
– Get all ‘Used’ Items
– Append Post ID’s to the array.ALM
– Pass the Post ID array to ALM
[ajax_load_more post__in="12, 14, 123, 454, 66, 324, 532, 32"]
Hope this makes a bit of sense ??
@dcooney
That was immensely helpful.I have the two queries working and building a comma delimited string of relevant post ids. However ALM does not appear to like this. If I just feed post__in the New items (216), it works great. If I add in or just do the Used (1,400), ALM loads nothing..not even the no records found message. ??
I should not be hitting the Apache or Php limits as they are both maxed out on script run time to temporarily support a migration (I am talking 512M of memory per script and 60+ minutes of run time).
I did try editing down my list of taxonomies and operators to the bare minimum in the shortcode since some of this had already been achieved by the custom queries, but that did not change the out come of ALM not display anything.
Any ideas?
Shortcode:
echo do_shortcode(‘[ajax_load_more container_type=”div”
post_type=”inventory”
taxonomy=”function:function:condition:status”
taxonomy_terms=”post-press:::sold”
taxonomy_operator=”IN:IN:IN:NOT IN”
order=”ASC”
orderby=”status”
post__in=’.$list_of_ids.’
posts_per_page=”30″
button_label=”Display More Listings”
no_results_text=”<h2>No listings found. </h2> <p>Please return to the previous page.</p>”
]’);Below is a sample of what $list_of_ids contains. I have tried eliminating putting these values directly into the ALM shortcode (no variable as used in the example above). I also tried eliminating the two custom queries since I already have the list of ids (for testing of course), but still could not get ALM to use the list of ids.
$list_of_ids = 4594,4612,4655,4694,4750,4765,4868,4877,4883,4907,4926,4938,4945,4953,4974,4994,4999,5052,5058,5098,5116,5121,5132,5171,5196,5222,5227,5243,5253,5263,5271,5278,5291,5304,5306,5327,5333,5344,5352,5363,5371,5417,5420,5425,5439,5454,5467,5471,5475,5496,5511,5548,5562,5571,5578,5585,5598,5683,5691,5695,5736,5883,5929,5945,5956,5967,5974,6002,6005,6015,6020,6027,6040,6046,6052,6059,6089,6100,6111,6118,6123,6148,6160,6167,6179,6184,6202,6227,6271,6276,6287,6299,6297,6308,6316,6320,6345,6352,6355,6361,6369,6372,6376,6447,6539,6571,6577,6598,6627,6635,6640,6643,6690,6701,6713,6754,6767,6790,6878,6885,6894,6910,6925,6931,6955,6981,6986,6998,7008,7020,7042,7046,7062,7081,7094,7111,7134,7139,7145,7166,7172,7188,7198,7202,7215,7220,7229,7240,7267,7301,7309,7333,7336,7361,7366,7371,7378,7389,7397,7400,7408,7431,7445,7449,7461,7465,7478,7485,7492,7499,7504,7510,7516,7518,7535,7541,7549,7554,7561,7580,7589,7598,7605,7607,7626,7634,7644,7650,7679,7684,7696,7700,7704,7709,7714,7722,7730,7735,7741,7771,7776,7781,7803,7812,7816,7820,7826,7837,7840,7846,7855,7861,7880,7888,7893,7909,7915,7929,7956,7961,7966,7973,7980,7987,8005,8027,8041,8044,8046,8056,8062,8069,8076,8090,8096,8100,8105,8111,8116,8147,8181,8189,8194,8206,8211,8222,8234,8236,8244,8250,8259,8275,8296,8302,8321,8389,8403,8417,8424,8430,8432,8441,8443,8448,8471,8473,8481,8487,8501,8510,8515,8525,8534,8540,8545,8556,8564,8567,8571,8578,8587,8599,8604,8611,8616,8620,8629,8643,8649,8662,8679,8684,8704,8710,8520,8715,8722,8732,8745,8750,8759,8766,8772,8779,8786,8800,8806,8830,8840,8861,8866,8872,8875,8886,8906,8912,8921,8928,8933,8937,8945,8953,8958,8964,8973,8986,8993,9000,9007,9029,9039,9048,9053,9061,9067,9074,9080,9086,9092,9105,9107,9121,9134,9137,9143,9147,9151,9167,9172,9174,9176,9193,9198,9203,9209,9219,9231,9235,9239,9266,9273,9279,9287,9297,9304,9308,9329,9334,8672,9348,9353,9361,9378,9384,9385,9393,9427,9436,9444,9454,9460,9490,9495,9497,9502,95
@dcooney no luck so far getting this to work.
Based on the feedback above and the roughly 1700 ids, am I overloading ALM?
1700 IDs is way too many. I’m surprised this didn’t crash your site. I’ll assume you’ve done something wrong since it didn’t.
If you send me a link I can look this week to see if you’ve managed to pass the post IDs correctly.
“Way too many” – I am assuming I should send ALM all the possible results for listing (via post ids) that it potentially display so it could further filter and “paginate” for scroll/load. Am I miss understanding this?
Site is likely not crashing because server has incredibly high memory allocation and script run time at the moment to support migration scripts. AKA values that will be turned back to normal apache/php limits post migration.
https://stage.bestgraphics.net/listings/?c=packaging I believe line 252 is what you are looking for.
While we are at it….here are is one of two of the custom queries. The other one just is almost the same but grabs those that are not “new”:
<?php
$args2 = array(
‘post_type’ => ‘inventory’,
‘posts_per_page’ => -1,
‘order’ => ‘ASC’,
‘no_found_rows’ => true,
‘fields’ => ‘ids’,
‘tax_query’ =>
array(
array(
‘taxonomy’ => ‘condition’,
‘field’ => ‘id’, // ‘term_id’,
‘terms’ => array(453), // NEW items
‘operator’=> ‘IN’ // ‘NOT IN’
)
),
);$query_posts2 = new WP_Query($args2);
if($query_posts2->have_posts()) :
while($query_posts2->have_posts()) : $query_posts2->the_post();
$new_listing_ids[]=get_the_ID();
endwhile;
wp_reset_query();
endif
?>Thank you!
Also,
I am still not clear on how using post__in allows me to sort by taxonomy or more importantly put listings part of a particular taxonomy and value at the top.
My ultimate goal it to ensure when listing are displayed by ALM, listings that are part of the custom taxonomy of Condition with a value of “new” are always at the top of the page of listings ALM creates.
- This reply was modified 3 years, 11 months ago by bobhunter.
I am still not clear on how using post__in allows me to sort by taxonomy or more importantly put listings part of a particular taxonomy and value at the top.
It won’t, you never mentioned this before.
You’re best to create that meta field for sorting. This isn’t going to work.
- The topic ‘Orderby Taxonomy’ is closed to new replies.