How to order posts list using wp_query? - wordpress

I want to order my list with wp_query. I have posts with meta_key 'featured_post' and three values for that key for different posts it is: '1', '2', and thrird key not exist.
I am using now this wp_query structure:
global $paged, $wp_query, $wp;
$args = wp_parse_args($wp->matched_query);
if ( !empty ( $args['paged'] ) && 0 == $paged ) {
$wp_query->set('paged', $args['paged']);
$paged = $args['paged'];
}
$temp = $wp_query;
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 20,
'paged' => $paged,
'meta_query' => array(
'relation' => 'OR',
'featured_clause_first' => array(
'key' => 'featured_post',
'value' => '1',
'compare' => '='
),
'featured_clause_second' => array(
'key' => 'featured_post',
'value' => '2',
'compare' => '='
),
'featured_clause_third' => array(
'key' => 'featured_post',
'compare' => 'NOT EXISTS',
'value' => '1'
)
),
'orderby' => array(
'featured_clause_first' => 'DESC',
'featured_clause_second' => 'DESC',
'featured_clause_third' => 'DESC'
),
);
$wp_query = null;
$wp_query = new WP_Query($args);
while ($wp_query->have_posts()) : $wp_query->the_post();
get_template_part( 'templates/single');
endwhile;
wp_reset_postdata();
wp_reset_query();
But I am got wrong order. Finally I need to see posts with value '1' at the top and other posts with values '2' and not exist key after.
Note: I can't use "'meta_key' => 'featured_post'," because in this way posts without existing meta_key not displayed.

Ok. I was edited save metabox functions for delete metakey if featured not checked. So that I have only two values: 1 & key not exist. After that I got needed order with this meta args:
$sort_meta_query = array(
'relation' => 'OR',
'featured_clause' => array(
'key' => 'featured_post',
'value' => '1',
'compare' => '='
),
array(
'key' => 'featured_post',
'compare' => 'NOT EXISTS',
'value' => '1'
)
);
$sort = array(
'orderby' => array(
'featured_clause' => 'DESC',
'date' => 'DESC'
)
);

Related

Wordpress - WP Query : meta_query relation ignored

I need help with a WP_Query. I use the meta query argument using the OR and AND relations, but this argument seems to be ignored in the result of the query.
Here is my code :
$args = array(
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'my_custome_post_type',
'posts_per_page' => 1,
'meta_query' => array(
'relation' => 'OR',
array(
'relation' => 'AND',
array( 'author' => $contact_id ),
array( 'meta_key' => 'my_meta', 'meta_value' => $user_id )
),
array(
'relation' => 'AND',
array( 'author' => $user_id ),
array( 'meta_key' => 'my_meta', 'meta_value' => $contact_id )
)
)
);
$query = new \WP_Query( $args );
$response = $query->posts;
I already tried to add this argument like suggested in here:
'suppress_filters' => false,
'is_your_custom_query' => true, // your custom flag
Even if I remplace the value of $user_id and $contact_id directly in the query with any number, the query still return the same result. I don't understand why it's not working.
Thank you for your help !
As suggested by dafoxuk, I have to remplace meta_key by key and meta_value by value. I also had to add 'compare' => 'LIKE'.
But event in this case it wasn't working. I also had to stock the author_id in a post_meta, and change the array( 'author'=> $contact_id ) condition by :
array( 'key ' => 'meta_author_id', 'value' => $user_id, 'compare' => 'LIKE' )
So the final $args array look like this :
$args = array(
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'my_custome_post_type',
'posts_per_page' => 1,
'meta_query' => array(
'relation' => 'OR',
array(
'relation' => 'AND',
array( 'key ' => 'meta_author_id', 'value' => $user_id, 'compare' => 'LIKE' )
array( 'key ' => 'my_meta', 'value' => $user_id, 'compare' => 'LIKE '
array(
'relation' => 'AND',
array( 'key ' => 'meta_author_id', 'value' => $user_id, 'compare' => 'LIKE' )
array( 'key ' => 'my_meta', 'value' => $contact_id, 'compare' => 'LIKE' )
)
);

Wordpress meta_query not returning results

I want a query that returns CPT posts that contains the user's input (which is $text).
Inserting meta_key and meta_value works well but putting it in a meta_query array doesn't return anything. I want to be able to search in multiple custom fields. It's a theme I made from scratch so there is no plugins and the functions.php file is pretty small so I don't think it could be a conflict.
Code for meta_key and meta_value in the query declaration (working):
$searchquery = new WP_Query(
array( 'post_type' => 'offre',
'meta_key' => 'offre_employeur',
'meta_value' => $text,
'meta_compare'=> 'LIKE' )
);
Code for meta_value array (not working):
$meta_query_args = array(
'relation' => 'OR',
array(
'key' => 'offre_employeur',
'value' => $text,
'compare' => 'LIKE'
), array(
'key' => 'offre_titre',
'value' => $text,
'compare' => 'LIKE'
)
);
$searchquery = new WP_Query(array('post_type' => 'offre'));
$searchquery->set('meta_query', $meta_query_args);
Code for the second way I tried but still no results (not working)
$args = array(
'post_type' => 'offre',
'posts_per_page' => -1,
's' => $text,
'meta_query' => array(
array(
'key' => 'offre_employeur',
'value' => $text,
'compare' => 'LIKE'
),
array(
'key' => 'offre_titre',
'value' => $text,
'compare' => 'LIKE'
)
)
);
$searchquery = new WP_Query($args);
Thank you in advance for your time.
Per the docs here: https://codex.wordpress.org/Class_Reference/WP_Query
I would assert you want to scroll down to the section titled "Multiple Custom Field Handling:", which has this example, which most closely matches your situation:
$args = array(
'post_type' => 'product',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'color',
'value' => 'blue',
'compare' => 'NOT LIKE',
),
array(
'key' => 'price',
'value' => array( 20, 100 ),
'type' => 'numeric',
'compare' => 'BETWEEN',
),
),
);
$query = new WP_Query( $args );
Which, taken what you've provided in your question, I would modify as follows to get the results you are after:
$args = array(
'post_type' => 'offre',
'posts_per_page' => -1,
// remove the "search" query, which restricts the results to those with titles / content that match the $text content
// 's' => $text,
'meta_query' => array(
// add the "relation" argument, default is AND, you need OR
'relation' => 'OR',
array(
'key' => 'offre_employeur',
'value' => $text,
'compare' => 'LIKE'
),
array(
'key' => 'offre_titre',
'value' => $text,
'compare' => 'LIKE'
)
)
);
$searchquery = new WP_Query($args);

Get all wp_users sort by metakey

I am trying to get users list sort by metakey. I mean i have set meta key user_last_login. some users doesnot have this meta key. some users have it.
I want to get both users in single query but if metakey exists those will come first and remaining should come last.
Here is my query
$meta_query_args = array(
'relation' => 'OR', // Optional, defaults to "AND"
array(
'key' => 'user_last_login',
'compare' => 'Not Exists'
),
array(
'key' => 'user_last_login',
'compare' => 'Exists'
)
);
$meta_query = new WP_Meta_Query( $meta_query_args );
$args = array(
'offset' => $paged ? ($paged - 1) * $number : 0,
'number' => $number,
'fields' => 'all',
'orderby'=>'user_last_login',
'exclude' => array(get_current_user_id()),
'meta_query' =>$meta_query
);
$users = get_users( $args );
Please help.
A meta_key argument is required when you wan to order by meta_value. Here is the modified code,
$query_args = array(
'relation' => 'OR',
array(
'key' => 'user_last_login',
'compare' => 'NOT EXISTS'
),
array(
'key' => 'user_last_login',
'compare' => 'EXISTS'
)
);
$meta_query = new WP_Meta_Query($query_args);
$args = array(
'offset' => $paged ? ($paged - 1) * $number : 0,
'number' => $number,
'fields' => 'all',
'meta_key' => 'user_last_login',
'orderby' => 'meta_value',
'order' => 'ASC',
'exclude' => array(get_current_user_id()),
'meta_query' => $meta_query
);
$users = get_users( $args );
Hope this one helps.

Wordpress: show future events based on custom field AND order by date

I have a custom content type (event), within that type I have a custom date field, with the name: 'date'. In my template I am running the WP_Query to return a list of events. I would like to only get events that are in the future (according to my custom 'date' field) and then order the list by that same field.
I have tried the following but it simply returns a list of all the 'events' in the system, regardless of date
$today = date('d M, y');
$args = array (
'post_type' => 'event',
'meta_query' => array(
array(
'key' => 'date',
'value' => $today,
'compare' => '>',
'type' => 'CHAR',
),
),
'meta_key' => 'date',
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
As a note: if I replace the type from 'CHAR' to 'DATE' I get no results returned...
Try this:
functions.php
function custom_unixtimesamp ( $post_id ) {
if ( get_post_type( $post_id ) == 'events' ) {
$startdate = get_post_meta($post_id, 'event_date_begins', true);
if($startdate) {
$dateparts = explode('/', $startdate);
$newdate1 = strtotime(date('d.m.Y H:i:s', strtotime($dateparts[1].'/'.$dateparts[0].'/'.$dateparts[2])));
update_post_meta($post_id, 'unixstartdate', $newdate1 );
}
}
}
add_action( 'save_post', 'custom_unixtimesamp', 100, 2);
then query
$today = time();
$args = array(
'post_type' => 'events',
'post_status' => 'publish',
'posts_per_page' => '10',
'meta_query' => array(
array(
'key' => 'unixstartdate',
'compare' => '>=',
'value' => $today,
)
),
'meta_key' => 'event_date_begins',
'orderby' => 'meta_value',
'order' => 'ASC',
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1 ),
);

Wordpress WP_Query with meta_query get values by Like

I'm building a custom search.
On this i'm trying to get posts where the searched text should be used as a wild card to compare within 2 meta_key.
Below is my code,
<?php $args = (
array(
'post_type' => 'registration',
'meta_key'=>'rg_upload_video',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'rg_first_name',
'value' => $s,
'compare' => 'LIKE'
),
array(
'key' => 'rg_last_name',
'value' => $s,
'compare' => 'LIKE'
)
),
'posts_per_page' => 12,
'paged' => get_query_var('paged') ? get_query_var('paged') : 1 )
);
$loop = new WP_Query($args);
?>
As a result it's simply returning all posts having Meta_key as rg_upload_video. I need the posts having searched text in meta_key rg_first_name or rg_last_name.
Did anyone know where i'm going wrong ?
You could do two WP_Query queries. In the first one you select all that has the rg_upload_video meta key. Then you do another WP_Query that searches only the posts obtained in the first query. Not tested, but it should look like:
$args = array(
'post_type' => 'registration',
'posts_per_page' => -1,
'meta_key'=>'rg_upload_video',
);
$videos = new WP_Query($args);
# get list of posts containing meta key rg_upload_video
$post_ids = wp_list_pluck( $videos->posts, 'ID' );
# You then do another WP_Query to get the posts having postmeta first name or last nav_menu_description
$args = array(
'post_type' => 'registration',
'post__in' => $post_ids,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'rg_first_name',
'value' => $s,
'compare' => 'LIKE'
),
array(
'key' => 'rg_last_name',
'value' => $s,
'compare' => 'LIKE'
)
),
'posts_per_page' => 12,
'paged' => get_query_var('paged') ? get_query_var('paged') : 1 )
);
$loop = new WP_Query($args);
Try running the below code.
<?php $args = array(
'post_type' => 'registration',
'meta_query' => array(
'relation' => 'AND',
array(
'relation' => 'OR',
array(
'key' => 'rg_first_name',
'value' => $s,
'compare' => 'LIKE'
),
array(
'key' => 'rg_last_name',
'value' => $s,
'compare' => 'LIKE'
)
),
array(
'key' => 'rg_upload_video',
'compare' => 'EXISTS'
)
),
'posts_per_page' => 12,
'paged' => get_query_var('paged') ? get_query_var('paged') : 1 )
);
$loop = new WP_Query($args);
?>

Resources