Cannot order destinations alphabetically - php

I have unordered destinations, which i print in the following way:
$destinations = get_posts( array(
'post_type' => 'destination_showcase',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'destination_state',
'value' => ':"'.$state_id . '";' , // looking for serialized value in quotes
'compare' => 'LIKE'
)
),
) );
I want to print the destinations alphabetically. When i var_dump-ed the #destinations array i got all of the destinations and their parameters. I want to get their titles i.e "post_title" and print it alphaberically. I've tried this, but doesn't work:
'orderby'=> $destinations->post_title, 'order' => 'ASC',
Any ideas how the task can be done ?

just try this
$destinations = get_posts( array(
'post_type' => 'destination_showcase',
'posts_per_page' => -1,
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'destination_state',
'value' => ':"'.$state_id . '";' , // looking for serialized value in quotes
'compare' => 'LIKE'
)
),
) );

If I read the WP Query manual properly, this should work:
get_posts( array(
'post_type' => 'destination_showcase',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'destination_state',
'value' => ':"'.$state_id . '";' ,
'compare' => 'LIKE'
)
),
'orderby' => 'title',
'order' => 'ASC',
) );

Related

Destinations aren't ordered by ascending. Why?

I'am pulling destinations from the database and trying to order them alphabetically in the following way:
$destinations = get_posts( array(
'post_type' => 'destination_showcase',
'posts_per_page' => -1,
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'destination_state',
'value' => ':"'.$state_id . '";' , // looking for serialized value in quotes
'compare' => 'LIKE'
)
),
'orderby' => 'title',
'order' => 'ASC',
) );
but did not succeed. Why ?
$destinations = get_posts( array(
'post_type' => 'destination_showcase',
'posts_per_page' => -1,
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'destination_state',
'value' => ':"'.$state_id . '";' , // looking for serialized value in quotes
'compare' => 'LIKE'
)
),
) );
try the order change

Visual composer custom query - excluding meta_key

I have a custom query that I would like some help converting to visual composer's custom query. Basically, I would like to exclude all posts from displaying in the post grid that have the meta_key: _is_featured_posts and its value as yes.
// WP_Query arguments
$args = array(
'post_type' => array( 'post' ),
'post_status' => array( 'publish' ),
'nopaging' => false,
'posts_per_page' => '12',
'order' => 'DESC',
'orderby' => 'date',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '_is_ns_featured_post',
'value' => 'yes',
'compare' => 'NOT EXISTS',
),
),
);
// The Query
$query = new WP_Query( $args );
Any help would be appreciated.
Thanks
There is an alternative solution, it is not recommended but as NOT EXISTS is not working so you can use the following code. I also check the solution given here, but it's not working either.
//to hold the post id which has _is_ns_featured_post => 'yes'
$exclude_id = array();
$args_exclude = array(
'post_type' => array('post'),
'post_status' => array('publish'),
'posts_per_page' => '-1',
'meta_query' => array(
array(
'key' => '_is_ns_featured_post',
'value' => 'yes',
),
),
);
$exclude_posts = new WP_Query($args_exclude);
if (!empty($exclude_posts->posts))
{
foreach ($exclude_posts->posts as $post)
{
$exclude_id[] = $post->ID;
}
}
$args = array(
'post_type' => array('post'),
'post_status' => array('publish'),
'nopaging' => false,
'posts_per_page' => '12',
'order' => 'DESC',
'orderby' => 'date',
'post__not_in' => $exclude_id //exclude post_id which has _is_ns_featured_post => 'yes'
);
// The Query
$query = new WP_Query($args);
foreach ($query->posts as $post)
{
print_r($post);
}
Hope this helps!
See: visual composer wordpress query for post grid
Try this:
$args = array(
'post_type' => array( 'post' ),
'post_status' => array( 'publish' ),
'nopaging' => false,
'posts_per_page' => '12',
'order' => 'DESC',
'orderby' => 'date',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => '_is_ns_featured_post',
'value' => 'yes',
'compare' => 'NOT EXISTS',
),
),
);
echo http_build_query($args);
// Result:
post_type%5B0%5D=post&post_status%5B0%5D=publish&nopaging=0&posts_per_page=12&order=DESC&orderby=date&meta_query%5Brelation%5D=AND&meta_query%5B0%5D%5Bkey%5D=_is_ns_featured_post&meta_query%5B0%5D%5Bvalue%5D=yes&meta_query%5B0%5D%5Bcompare%5D=NOT+EXISTS
http://sandbox.onlinephpfunctions.com/code/5c2bc6ddd37a02fc8facf4f227176e262854b92e
I would recommend to avoid use array('post') in case if only one post type, so just use post_type=post&post_status=publish&nopaging=0&posts_per_page=12&order=DESC&orderby=date&meta_query[relation]=and&meta_query[0][key]=_is_ns_featured_post&meta_query[0][value]=yes&meta_query[0][compare]=NOT EXISTS
P.S.
possibly %5B and %5D you will need to convert back to [ and ] via echo urldecode(http_build_query($args));

Wordpress WP_Query with multiple meta_values for a meta_key

I'm trying to run a WP_Query in order to search for all the products in my database with multiple meta values.
e.g
Product 1 -> meta_key['key1'] ->meta_value['value1']
Product 2 -> meta_key['key1'] ->meta_value['value2']
Product 3 -> meta_key['key1'] ->meta_value['value3']
So i want to get all three products.My arguments are
$args = array(
'post_type' => 'product',
'posts_per_page' => 2,
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'key1',
'value' => 'val1',
'compare' => '='
),
array(
'key' => 'key1',
'value' => 'val2',
'compare' => '='
),
array(
'key' => 'key1',
'value' => 'val3',
'compare' => '='
),
),
'paged' => $paged
);
The problem is that no products are returned. Instead , if a give only one meta_key => meta_value pair it works fine
$args = array(
'post_type' => 'product',
'posts_per_page' => 2,
'orderby' => 'title',
'order' => 'ASC',
'meta_key' => 'key1',
'meta_value' =>'val1',
'paged' => $paged
);
You should use IN as compare value, like this:
$args = array(
'post_type' => 'product',
'posts_per_page' => 2,
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'key1',
'value' => array('val1','val2','val3'),
'compare' => 'IN',
),
),
'paged' => $paged
);
Hope it helps!

Exclude posts from get_posts()

Good morning, I found many similar questions, but none of the answer fit to my problem. The point is very simple: I have a custom loop with get_posts(), and I want to exclude current post from being displayed.
The code is:
$args = array(
'posts_per_page' => 3,
'orderby' => 'meta_value',
'order' => 'ASC',
'post_type' => 'fasthomepress_pt',
'post__not_in' => array(get_the_id()),
'meta_query' => array(
array(
'key' => 'custom_richiesta',
'value' => array($custom_boxes['custom_richiesta'][0] - 10000, $custom_boxes['custom_richiesta'][0] + 10000 ),
'type' => 'numeric',
'compare' => 'BETWEEN'
)
)
);
I tried with:
'post__not_in' => array(get_the_ID),
'post__not_in' => array($post->ID),
'exclude' => $post->ID,
'exclude' => get_the_ID,
and with many other combinations with or without array. Of curse, current post id is correctly echoed before this loop, and if I try echo($post->ID) and echo(get_the_ID()) I have the same, correct, result.
I really don't know what's happening,
thank you very much for help,
Marco
Try exclude.
$args = array(
'posts_per_page' => 3,
'orderby' => 'meta_value',
'order' => 'ASC',
'post_type' => 'fasthomepress_pt',
'exclude' => array(get_the_id()),
'meta_query' => array(
array(
'key' => 'custom_richiesta',
'value' => array($custom_boxes['custom_richiesta'][0] - 10000, $custom_boxes['custom_richiesta'][0] + 10000 ),
'type' => 'numeric',
'compare' => 'BETWEEN'
)
)
);
here is a function that does just that:
function get_lastest_post_of_category($cat){
$args = array( 'posts_per_page' => 1, 'order'=> 'DESC', 'orderby' => 'date', 'category__in' => (array)$cat);
$post_is = get_posts( $args );
return $post_is[0]->ID;
}
Usage: say my category id is 22 then:
$last_post_ID = get_lastest_post_of_category(22);
you can also pass an array of categories to this function.
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 18,
'paged' => $paged,
'offset' => 0,
'post__not_in' => array($last_post_ID,),
'category' => '',
'category_name' => '',
'orderby' => 'post_date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
// The Query
$the_query = new WP_Query( $args );

Order by One Custom Field Value and Select Posts From Another With Wordpress

I'm trying to only show posts that are marked as in-stock and order them by their inventory_number (which is a number value so I'm using meta_value_num). The code below is selecting in-stock items, but it isn't ordering the posts by inventory_number. What am I doing wrong?
$args = array(
'numberposts' => -1,
'post_status'=>"publish",
'post_type'=>"post",
'category_name'=>"tape",
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'inventory_number',
'orderby' => 'meta_value_num',
'order' => 'asc'
),
array(
'key' => 'status',
'value' => 'in-stock',
'compare' => 'LIKE'
)
)
);
For order by on a custom field, meta_key=keyname must be present in the query. Plus I don't think you want the order by in the AND clause. So try this...
$args = array(
'numberposts' => -1,
'post_status' => 'publish',
'post_type' => 'post',
'category_name' => 'tape',
'meta_query' => array(
array(
'key' => 'status',
'value' => 'in-stock',
'compare' => 'LIKE'
)
),
'meta_key' => 'inventory_number',
'orderby' => 'meta_value_num',
'order' => 'ASC'
);

Categories