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',
);
Related
I have a WP_Query like so:
<?php $args = array(
'post_type'=> array('post', 'page'),
'cat'=> 108,
'posts_per_page' => 10,
'offset' => 4,
'meta_key' => 'post_views_count',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'date_query' => array(
array(
'after' => '3 months ago'
)
),
);
query_posts( $args ); ?>
What I want is are 10 posts that:
Have the highest value for post_views_count
Are not the 4 most recent posts published.
But it seems that 'offset' => 4 is running the query, ordering by post_views_count and then skipping 4 of those, instead of the most recently published 4.
Would anyone have any insight into how I can it to skip the first 4 and then run my meta_key query?
If you get somewhere earlier 4 first published posts you must put their ids in array and use query arg
'post__not_in' => $exclude_posts_arr
instead of
'offset' => 4,
If you didn't get this post earlier you can create another query with this parameters.
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 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
May I know whether the query below is correct? I would like to search for post that contain the title 'hello' using meta_query with various variables
$sq = new WP_Query(array( 'meta_query' => array(array('key' => 'post_title', 'value' => array('%hello%'), 'compare' => 'LIKE')), 'post_type' => 'post', 'posts_per_page' => $num, 'post_status' => 'publish', 'order' => $order, 'orderby' => $orderby, 'ignore_sticky_posts' => 1, 'cat' => $cats, 'offset' => $offset));
Thanks.
get_page_by_title() is usually NOT case sensitive. It only would be if the database was set to be case sensitive, which is unusual for MySQL.
Instructions for use of get_page_by_title() can be found at: developer.wordpress.org
Be sure to set the third parameter if you are wanting posts and not pages.
I'm struggling to get a working solution with this wp_query. I currently have some custom settings which are assigned to posts, one is whether or not the post is 'featured' and the second is a date and time for the post to end (no longer display in the results). I have the query working with the feature, but just need to work this end date into it, here is the query working find with the 'featured':
WP_Query('meta_key=skyali_feature&showposts=4&orderby=post_date');
The end date is set in the wp_postmeta table where meta_key is 'the_date' and the meta_values look like this '05/16/2013 05:24'. I would like to edit the above query where if 'the_date' has been set posts are only included if the 'the_date' is greater that todays date and time.
Here is my failed attempt:
WP_Query(
'meta_key=skyali_feature&meta_key=the_date&meta_compare=>=&meta_value='
.date('d/m/Y H:i')
.'&showposts=4&orderby=post_date&orderby=the_date'
);
I had to do something very similar recently and ended up needing to use the meta_query property instead. You'll want to do something like this:
$today = date('Ymd');
$args = array(
'post_type' => 'post',
'posts_per_page' => '4',
'meta_key' => 'the_date',
'meta_query' => array(
array(
'key' => 'skyali_feature'
),
array(
'key' => 'the_date',
'value' => $today,
'compare' => '>='
)
),
'orderby' => 'meta_value_num',
'order' => 'ASC'
);
$your_custom_query = new WP_Query($args);
A few notes...
I only needed to filter by date in my example, but it looks like you'll need to do date/time in yours. (You can just adjust the first line for the $today variable using the format you wish).
Use posts_per_page instead of showposts. showposts is deprecated.
Notice that I have included the meta_key twice (once at the top level of the array and once as an element in the meta_query array. There's a known bug where you can't sort your results by the key if you don't include it this way. I fought that one for a while too!
Hope this helps, have fun!
[edit] Forgot to add your skyali_feature key back into the array.
for people using the advanced custom field plugin with a date data type, this is what you need for dates greater or equal than today:
$today = date('Ymd');
$args = array(
'post_type' => 'post',
'meta_key' => 'end-date',
'meta_query' => array(
array(
'key' => 'end-date'
),
array(
'key' => 'end-date',
'value' => $today,
'compare' => '>='
)
),
'orderby' => 'meta_value',
'order' => 'ASC'
);
$your_custom_query = new WP_Query($args);
for people using Custom metadata manager you'll find that a datepicker field is stored as timestamp.
So in a similar case the above example isn't working and you may have php sort out the value you need to compare against. And the timestamp for a day earlier at 23:59:59 it'll do the job:
$yesterday = strtotime('yesterday 23:59:59');
$args = array(
'post_type' => 'post',
'meta_key' => 'end-date',
'meta_query' => array(
array(
'key' => 'end-date'
),
array(
'key' => 'end-date',
'value' => $yesterday,
'compare' => '>='
)
),
'orderby' => 'meta_value',
'order' => 'ASC'
);
$your_custom_query = new WP_Query($args);
If you also want to take account of the timezone setting for the blog use current_time() as in the following example:
$now = current_time('timestamp');
$yesterday = mktime(23, 59, 59, date('n',$now), date('j',$now)-1);