it seems i can not find what i am looking for anywhere.
i am using the code below to show posts, using their ids.the problem is that i can't sort them the way i want - i need to order them exactly in order of the ids (currently it is sorting asc or desc.
i don't need that, i only want to show them exactly in the order of the ids)
<?php
$query_args = array('post_type'=>'post', 'category' => '3', 'numberposts'=>'50', 'post_status'=>'publish', 'include' => '100,124,122');
$query_posts = get_posts($query_args);
$last_query = end($query_posts);
foreach ($query_posts as $post) : setup_postdata($post);
any ideas?
I was halfway through writing an answer when I found wyfel's answer to a similar question on the WordPress stack exchange. Short of adding a global to store the IDs (rather than hard-coding them), it's what I planned to suggest (even down to the MySQL FIND_IN_SET function).
Related
I have a small problem in a wordpress project that I have worked in the past, the solution that I applied there for a custom plugin was to create a couple of tables for some projects that the admin has the ability to add. My mistake was to not make taxonomies and register them in wordpress. Now those projects must appear in the search results. My question is: How/where can I write some code to include a custom table in the search ( where 'q' LIKE %title% ) to apply for the internal search and let wordpress calculate the pagination and show the results? Thank you
Wordpress had capability to use native mysql query. So you just use the native query to search
$querystr = "
SELECT *
FROM posts
WHERE post_title LIKE %title%
ORDER BY posts.post_date DESC";
$pageposts = $wpdb->get_results($querystr, OBJECT);
foreach ($pageposts as $post):
echo $post->post_title;
endforeach;
About paging, count the row and do paging manually. I think you can do that.
I want to get post by get_post order by meta_value_num and meta keys , I tried this code:
get_posts(
array( 'post_type' => 'posts',
'order' => 'ASC',
'orderby' => 'meta_value_num',
'meta_query' => array(
array('key' => 'class'),
array('key' => 'chair')
)
)
);
I want to get the post sort by class's number then chair's number .
but htis code not working how can this be done?
Although the new orderby parameter is great in WP_Query, it does not support multiple orderby for multiple meta_key's.
I've went through a couple of scenarios and even went and digged into trac and make and came up with the following
make.wordpress.org A more powerful orderby in wordpress 4.0
trac ticket #17065
None of the issues regarding this very problem have been answered. It also seems from those two links that there is an issue ordering by a meta_key and another field like post date for instance. I haven't tried this as yet.
I truelly think that you have two choices here without creating two queries.
hack using the posts_orderby filter as described by #s_ha_dum in his answer here
By making use of PHP ordering using usort. The idea here would be to sort your query by one meta_key and then take the returned posts array ($posts) and sort them by the second meta_key before the loop starts. You can either use the the_posts filter (just remember to remove the filter once done) or simply unset $posts in your template and set it with the reordered array of posts once done
Im aware there are several posts relating to this issue but i am yet to find a fix for my current problem.
Im reasonably new to Wordpress and am still getting used to it, however...
The Problem
I'm implementing a front end sorting system for users to filter out schools, i am using a rating plugin (comment rating field pro) and the issue i have is that when users sort by star rating i also want it to sub sort by number of reviews (comments). Currently it is only sorting by rating and simply ignoring the number of comments and sorting by title. I have tried various combinations of query parameter arrays such as meta_query and multiple meta keys but the issue appears to be that it refuses to sort the second value. Both are numbers in the database.
TL:DR it needs to sort by stars and then by number of reviews.
Unfortunatly a custom query is out of the question as the array is being fed into another plugin which appears to only accept an array of args.
This is the sort of idea im going for (this does not work)
Array ( [post_type] => surf-school [posts_per_page] => 12 [paged] => 0 [meta_key] => crfp-average-rating [orderby] => meta_value crfp-total-rating [order] => DESC )
Any help would be greatly appreciated as its driving me insane!
Thanks in advance.
Update
I have managed to fix this using from a comment reply to this post which now appears to have been deleted :(
I managed it by adding a filter to wp_query and forcing a custom order by as seen here Plugin API/Filter Reference/posts orderby - Codex
function edit_posts_orderby($orderby_statement) {
$orderby_statement = "wp_postmeta.meta_value DESC, wp_posts.comment_count DESC";
return $orderby_statement;
}
placed in functions.php and
add_filter('posts_orderby', 'edit_posts_orderby');
placed just before the query. Hope this helps someone else :)
my question has to do with the pods framework plugin for wordpress sites. I am using pods version 2.2 and have been having trouble with the where parameter in the find() function.
I'd be suprised if I am the first person to encounter this problem but I have searched extensively and haven't found anyone providing an answer (or question at that).
Anyway, I'll give an example to highlight my problem.
Say I have a Bands Pod and a Records Pod and these two pods have a bi-directional multi-select relationship between them (that is, an n to n relationship). Hence a band can have numerous records and a record can have multiple bands. Further, the relationship exists between the fields BandsPod('records') and RecordsPod('bands').
Now, I can retrieve all records in the records pod like so (note the where is commented out):
$pods = pods('records');
$params = array(
'select' => 't.*',
'limit' => -1
//,'where' => '???'
);
$pods->find($params);
Then do whatever I want with them, e.g. template(), fetch(), etc.
My trouble occurs when I want to filter by band. What should my where statement be if I want to retrieve all records by band with id 1?
My view is that it should be something like this:
$params = array(
'select' => 't.*',
'limit' => -1,
'where' => '1 IN bands.id'
);
$pods->find($params);
However, this does not work (not that I especially expected it to).
It would also be desirable to know how this would work for filtering by multiple bands. E.g.
'where' => '(1, 2) IN bands.id'
As I said before, I've been trying to get this to work for some time and with little joy. I have managed to get it working but in a very ugly way. I.e.
Get bands from Bands Pod with relevant band ids,
Collect all ids of records from the bands pod records field,
Write params for Records Pod
Use the ids in the where statement to check they match with t.id,
$pods->find(); //where $pods=pods('records');
Thanks in advance for taking the time to read this and any answers you may give.
Cheers,
Joe
N.B. I know about the filters() function and it does not do what I'm after. I'd like stuff specifically for the where statement please.
You would want:
'where' => 'bands.id IN ( 1, 2 )'
If your bands are a custom post type, it would be:
'where' => 'bands.ID IN ( 1, 2 )'
I am getting a collection of categories using
foreach(Mage::getModel('catalog/category')->getCollection() as $category)
For each of these categories, I need to retrieve the sort order. How do I do this?
The only function I can find that looks relevant in the category class is getDefaultSortBy(), which always returns news_from_date, which is neither the default sort order (position) or the selected sort order (price), so I don't know where that's getting its value from. I also noticed that if, in the magento backend, I change the list of available sort orders, the function getAvailableSortByOptions still always returns the same array. From these two facts, I conclude that the category functions must be accessing some sort of cross-category global settings, which is useless to me.
I want the specific sort order chosen for each specific category. Is there any way to retrieve this? Or do I need to write my own SQL? In that case, which table do I need to query?
I am using magento enterprise ver. 1.11.1.0
Found the answer. Naturally, moments after I posted the question!
The problem is that Mage::getModel('catalog/category')->getCollection() does NOT automatically load all of the category attributes. You have to indicate which ones to retrieve manually. So, I need to replace this:
foreach(Mage::getModel('catalog/category')->getCollection() as $category)
With this:
foreach(
Mage::getModel('catalog/category')->getCollection()
->addAttributeToSelect('default_sort_by')
->addAttributeToSelect('available_sort_by')
as $category
)