Change wordpress to display search results in chronological order - wordpress

This is a Thesis theme site using the core WP search functionality. Not looking for a new plugin solution, just how to alter the search results to change order of posts from relevance to chronological order. Any help is greatly appreciated.

Some of native wp search results, like terms do not have any type time info stored. You can use WP_User_Query with order of records. It will be not exactly what you expected but will have chronological order:
$args = array(
'search' => 'search_patern_from_form',
'orderby' => 'id',
'order' => 'DESC'
);
$user_query = new WP_User_Query( $args );
See order by section of WP_User_Query codex page

Related

I cant get wordpress to sort advanced custom field sort by date

Hoi
i have a script, that have to order some post in wordpress. I youe Advanced Custom Fileds. The Field-group is called Kalender, and the date filed is called dato.
I try to order the list by this
$custom_query = new WP_Query(
array('posttype' => 'kalender',
'orderby'=> 'meta_value',
'meta_key'=> 'dato',
'order' => 'DESC'));
But that is not sorting the posts after date.
Can anyone tell me that is wrong?

Wordpress: Custom-Field and multiple orderby

I have defined several custom post types (CPTs) with some Custom Fields (CF) and need to have a certain sorting - first by category, then by date, something along the lines of
$args = array (
'meta_value category' => 'ASC',
'meta_value startdate' => 'ASC'
),
'post_type' => 'training'
);
$the_query = new WP_Query($args);
However, this does not seem to work and the dates are not correctly parsed, since the default parsing of a meta_value is alphabetical. I cannot set the meta_type to DATE, because there are two meta_values.
Any ideas?
So, I was unable to find a solution. Instead I wrote a custom function in php that took the database result and sorted it to my needs.

Wordpress filters

I used wordpress to make a website with some custom types. The problem is that i need to implement a filter for these types and I have some questions about the plugins that i'm using for this project:
For to make custom types and views I'm using Types and Views plugins for wordpress.
I made a new custom type with some fields (title, price, some selectors, ...) and I need to filter all the information ordering the results following 2 criterias. I don't know if is it possible to do so using the Views/Types plugin.
I also need to filter the fields of the filter. For example, if i have 2 fields one called Countries and other called Cities, if i select Spain in countries I should see only spanish cities in the second field.
Any idea if i can do that using Types and Views plugins? is there any other plugin?
Thanks in advance!
Any good plugin will save the custom fields as post-meta. You need to find out what the key is for the field you are trying to use as a filter. Once you have that you can use a simple meta query to get the posts you need:
$args = array(
'post_type' => 'post', // The post type
'posts_per_page' => -1, // Get all posts
'orderby' => 'rand', // Random order, also can use 'post_date' or 'post_title'
'order' => 'ASC', // Direction of sort
'meta_key' => 'my_meta_key', // Whatever your key is
'meta_query' => array(
array(
'key' => 'my_meta_key', // Whatever your key is
'value' => 'foo' // What it should be
'compare' => 'IN', // How to compare the value to the stored value
)
)
);
$query = new WP_Query($args); // The query
Here is more on the Wordpress codex.

Wordpress WP_Query call posts AND pages

I have a feature slider set up that draws in posts that are tagged 'feature'
$my_query = new WP_Query(array(
'showposts' => 3,
'tag' => 'feature' ));
Is is possible to draw in posts AND pages?
I know you can draw pages with 'post_type'=>'page' but can you mix the two?
You can specify an array value for the post_type parameter, as such:
$my_query = new WP_Query(array(
'post_type' => array('post', 'page'),
'tag' => 'feature'
));
See this page for more info: WP Codex
#fivedigit Thanks but I went with this in the end:
$my_query = new WP_Query(array(
'post_type' => array('any'),
'tag' => 'feature'
));
Although your version may come in handy in the future!
For anyone having to edit older code that doesn't use an array passed into WP_Query, you can add &post_type=any to get posts and pages (and other content). Unfortunately I don't see a way to get posts and pages (without other types) without using an array, since post_type would then require an array as the examples above show. However, this should be good enough if you are searching for a particular category anyway.
Example (this from vSlider v4.1.2 where &post_type=any is added so that pages are included in the slider):
$recent = new WP_Query($randimg."cat=".$options['imgCat']."&showposts=".$options['slideNr']."&post_type=any");
Thanks to #fivedigit and #my-jonny-wood for the answers above that led me to figuring this out and fixing the slider on my site!

Wordpress search custom fields

I have a custom field with meta_name is product_id. I am trying to determine how to search that field when using the wordpress search.
I know the wordpress search results page uses query_string which contains the "s" value for the keyword searched I just don't know how to change my code below to search the custom field mentioned.
$loop = new WP_Query("$query_string . '&posts_per_page=-1&post_type=product'"); ?>
think this section in the codex should have the answers you need with regards to adding in custom field parameters to you query:
http://codex.wordpress.org/Function_Reference/WP_Query#Custom_Field_Parameters
Not sure whether that will allow you to search within a particular custom field though. Can't be certain but I think you'd have to make some changes on the search form end of things. See how you get on with adding in the custom field parameters.
Off the top of my head I would think you would need something like:
$query = new WP_Query( array(
'meta_key' => 'product_id',
'meta_value' => $query_string,
'posts_per_page' => '-1',
'post_type' => 'product'
) );
Not tested though!

Resources