small question. I need to specify multiple meta keys and values in wordpress get_post function. Can anyone tell me how to do that?
<?php $args = array(
'posts_per_page' => 5,
'offset' => 0,
'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
);
$posts_array = get_posts( $args ); ?>
Okay, never mind, found my solution in the documentation. If someone needs it,
$args = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => 'featured',
'value' => 'yes',
)
)
);
$postslist = get_posts( $args );
Related
I have two custom fields created with Advanced Custom Fields.
One is type of checkbox (isEvent) and another is type of date (closing_date).
If I want to get all the posts that are events I would do something like this
<?php $args = array(
'posts_per_page' => 7,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => '',
'order' => '',
'include' => '',
'exclude' => '',
'meta_key' => 'is_event',
'meta_value' => 'a:1:{i:0;s:4:"true";}',
'post_type' => 'events',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'post_status' => 'publish',
'suppress_filters' => 0
);
$my_posts_array = get_posts( $args );
And this is working.
But If I would like to sort by closing_date which is the custom field of type date, in the ACF documentation for sorting is suggested that i should do something like this:
<?php $args = array(
'posts_per_page' => 7,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'meta_value_num',
'order' => 'asc',
'include' => '',
'exclude' => '',
'meta_key' => 'closing_date',
'meta_value' => '',
'post_type' => 'events',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'post_status' => 'publish',
'suppress_filters' => 0
);
$my_posts_array = get_posts( $args );
This is not working. Update - I managed to get this thing to work. I did not change anything, after few tests it worked....
Can someone give answer on any of these questions (or all) ?
What is the way to have two custom fields in wordpress and you want
to sort by one and filter by another ?
Is this possible in one call of get_posts or is there any other
native wp technique?
Can get_posts have meta_key, meta_value and meta_query in same
argument list for get_posts?
I also want to add also that I am using the WPML translation plugin with 2 languages en and fr.
The way to write the query is
$my_posts_array = get_posts( array(
'post_type' => 'events',
'posts_per_page' => '7',
'offset' => 0,
'post_status' => 'publish',
'suppress_filters' => 0,
'meta_query' => array(
array(
'key' => 'is_event',
'value' => 'a:1:{i:0;s:4:"true";}',
'compare' => '='
)
),
'orderby' => 'meta_value_num',
'meta_key' => 'closing_date',
'order' => 'ASC',
)
);
2.Yes this is possible in one call try above code or if not woeking then try this.
$my_posts_array = get_posts( array(
'post_type' => 'events',
'posts_per_page' => '7',
'offset' => 0,
'post_status' => 'publish',
'suppress_filters' => 0,
'meta_query' => array(
array(
'key' => 'is_event',
'value' => 'a:1:{i:0;s:4:"true";}',
'compare' => '='
)
),
)
);
// The Loop
foreach ($my_posts_array as $key => $value) {
$allowed_posts[] = get_the_ID();
}
$sorted_array = get_posts( array(
'post_type' => 'events',
'posts_per_page' => '7',
'offset' => 0,
'post_status' => 'publish',
'suppress_filters' => 0 ,
'post__in' => $allowed_posts,
'orderby' => 'meta_value_num',
'meta_key' => 'closing_date',
'order' => 'ASC',
);
foreach ($sorted_array as $key => $value) {
//do the stuff here
}
OK,
I managed to answer to this questions by myself.
1. This is one way of doing it:
<?php $args = array(
'posts_per_page' => 7,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'include' => '',
'exclude' => '',
'meta_key' => 'closing_date',
'meta_value' => '',
'post_type' => 'events',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'post_status' => 'publish',
'meta_query' => array(
array('key' => 'is_event',
'value' => 'a:1:{i:0;s:4:"true";}'
)
),
'suppress_filters' => 0
);
$my_posts_array = get_posts( $args );
2. I assume you can accomplish this in similar fashion with WP_Query since get_posts i actually using this method.
3.Answer is Yes like in the above example.
The following is returning no results. I want to filter by category and multiple tags. Can you see what i am doing wrong?
$tags = array( "blah-blah", "sausage" );
$posts = get_posts( array(
'posts_per_page' => 3,
'offset' => 0,
'category' => $categoryID,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true,
'tag' => implode( ",", $tags )
) );
EDIT
This seems to work!:
$posts = get_posts( array(
'posts_per_page' => 3,
'offset' => 0,
'category' => $categoryID,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true,
'tag_slug__in' => $tags
) );
Try something like this :
$posts = get_posts( array(
'posts_per_page' => 3,
'offset' => 0,
'category__and' => $categoryID,
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true,
'tag__in' => $tags
) );
Check if it works.
See below example to get posts by tags & category
global $wp_query;
$args = array(
'category__and' => 'category',
'tag__in' => 'post_tag', //must use tag id for this field
'posts_per_page' => -1); //get all posts
$posts = get_posts($args);
foreach ($posts as $post) :
//do stuff
endforeach;
I have a wordpress loop with an array of arguments to show only specific posts (any posts with a deposit_amount value of 0).
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
'cat' => '11',
'meta_key' => 'deposit_amount',
'meta_value' => 0
);
$loop = new WP_Query( $args );
?>
I would like to create a similar array but showing posts with a deposit_amount meta_value of greater than 0
i have tried to use the php greater than operator but breaks the code.
'meta_value' => >0
Can anyone point me in the right direction with this problem?
just found 'meta_compare' => '>'
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
'cat' => '11',
'meta_key' => 'deposit_amount',
'meta_value' => 0,
'meta_compare' => '>'
);
$loop = new WP_Query( $args );
?>
Use a Meta Query:
$args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => 'ASC',
'cat' => '11',
'meta_query' => array(
array(
'key' => 'deposit_amount',
'value' => 0,
'compare' => '>'
)
)
);
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 );
In Wordpress I am trying to get all posts within multiple categories. Heres my code:
<?php
// Get categories
$menu = 'left-column-menu';
$cat_args = array(
'order' => 'ASC',
'orderby' => 'menu_order',
'post_type' => 'nav_menu_item',
'post_status' => 'publish',
'output' => ARRAY_A,
'output_key' => 'menu_order',
'nopaging' => true,
'update_post_term_cache' => false );
$cat_items = wp_get_nav_menu_items( $menu, $cat_args );
// Get categories posts
$args = array(
'posts_per_page' => '',
'offset' => 0,
'category' => $cat_items->ID,
'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 );
$posts = get_posts($args);
print_r($posts);
?>
The first part is getting the categories from a menu, this is working fine. But its only getting the first 5 posts. I'm just wondering why this is?
Change:
// Get categories posts
$args = array(
'posts_per_page' => '',
To:
// Get categories posts
$args = array(
'posts_per_page' => -1,