Is there a way to setup a custom WordPress query to get specific post IDs plus children of other specific posts IDs in the same query? I've tried using post_parent__in and post__in parameters but they stop working when combined.
I think this query would help you to achieve your functionality.. Try this query to get all those posts...
$parent = 2; //change as desired or get all parent post ids
$child_args = array(
'post_type' => 'post',
'post_parent' => $parent
);
$keys = array($parent);
$ids = array_merge($keys, array_keys( get_children( $child_args ) ));
$query = new WP_Query(
array(
'post_type' => 'post',
'post_status' => 'publish',
'post__in' => $ids,
'posts_per_page' => -1
)
);
Related
I want to show a random selection of 4 new products in WooCommerce.
For that I'm using a first loop to get the 20 newest products.
Like this:
$args= array(
'post_type' => 'product',
'posts_per_page' => 20,
'orderby' => 'date',
);
Now I've a second loop to reduce the products to 4 in a random order:
$args_new = array(
'posts_per_page' => 4,
'orderby' => 'rand',
);
In the end I merge the two loops:
$final_args = array_merge( $args, $args_new );
But that doesn't work. Is there any other way to achieve it?
General knowledge
The post_type argument accept String or Array.
Argument
Description
post_type
(String/Array) – use post types. Retrieves posts by post types, default value is post. If tax_query is set for a query, the default value becomes any.
Source # https://developer.wordpress.org/reference/classes/wp_query/#post-type-parameters
Merging queries
In crude terms we want to combine 2 posts queries, retrieve each posts ID, push them to a new array and open a new query.
Keep in mind that if you want your 4 posts to be random (as you stated in the comments) they might be some duplicates of the last 20 from the first query. Don't forget to offset the second query.
<?php
// First query
$args_1 = get_posts( array(
'post_type' => 'dogs',
'post_status' => 'publish',
'post_count' => 20,
) );
// Second query
$args_2 = get_posts( array(
'post_type' => 'dogs',
'post_status' => 'publish',
'post_count' => 4,
'offset' => 20,
'orderby' => RAND,
) );
// Merge queries
$posts = array_merge( $args_1, $args_2 );
// Push posts IDs to new array
$identifiers = array();
foreach ( $posts as $post ) {
array_push( $identifiers, $post->ID );
};
// Third query
$query = new WP_Query( array(
'post_status' => 'publish',
'post_count' => 24,
'post_in' => array_unique( $identifiers ),
) );
var_dump( $query );
Is this possible, I couldn't find the question asked anywhere on the web.
If you had a list of posts by their slug, example:
$postslugs = array("page-one","page-two","page-three");
And you wanted to create a single query to find all these specific pages:
$args=array(
'post__not_in' => array($post->ID),
'posts_per_page'=> -1,
'post_type' => 'customtype',
'post_status' => 'publish',
'pagename' => $postslugs
);
$my_query = new wp_query( $args );
The result should come back with the three pages. This of course would not work the way it's written now. Is this possible?
If you take a look at the documentation for the WP_Query() class, specifically the Post & Page Parameters section, you'll see it has a handy parameter called post_name__in which takes an array of post_name (aka slugs). Also, are you sure you need to use post__not_in (and maybe posts_per_page since you're defining an array of slugs to get already?
$names = array( 'page-one', 'page-two', 'page-three' );
$query_args = array(
'posts_per_page' => -1,
'post_type' => 'customtype',
'post_status' => 'publish',
'post_name__in' => $names
);
$my_query = new WP_Query( $query_args );
I would like to notice, that there is some issue with old WordPress versions where post_name it is not = to a post_slug.
For example if you try to create new post with title Customer.io it will create a post with name customer-io but slug will be customerio the same will be for this Pokéapi
The same will be if you change post slug after you have created the post.
So the best way is to check manually inside loop of your WP_Query.
$desired_array_of_slugs = array( 'page-one', 'page-two', 'page-three' );
$query_args = array(
'posts_per_page' => -1,
'post_type' => 'customtype',
'post_status' => 'publish'
);
$my_query = new WP_Query( $query_args );
Then
foreach($my_query as $post) {
if(in_array(sanitize_title($post->name)], $desired_array_of_slugs, true)){
//do ur staff
}
}
my first query is okay
$ids = [];
$novidades = get_posts( array(
'posts_per_page' => 4,
'meta_key' => 'meta-checkbox',
'meta_value' => 'yes'
) );
if ( count( $novidades ) ) {
foreach( $novidades as $novidade ) {
$ids[] = $novidade->ID;
}
}
//rest of my code is ok
but, i try post another post and ignore the first query, but don't work, list all post
$args2 = array(
'post_type' => 'post',
'posts__not_in' => $ids
);
$featured = new WP_Query($args2);
Can help me?
It's post__not_in. Remove the extra s from your code.
post__not_in (array) - use post ids. Specify post NOT to retrieve. If this is used in the same query as post__in, it will be ignored.
Your code should be:
$args2 = array(
'post_type' => 'post',
'post__not_in' => $ids,//<====extra 's' removed
);
$featured = new WP_Query($args2);
I am writing a custom plugin and I need to automatically send information about every newly created coupon. Until now, I can only choose a specific coupon by its name(e.g. 1234):
$coupon = new WC_Coupon("1234");
But I cannot seem to find how to get a coupon right after its created, without knowing its name, or at least how to get all of the available coupons. Can someone help?
May be this might help. Tested and it's working. This will return all coupons since coupons are saved as post_type shop_coupon.
$args = array(
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'asc',
'post_type' => 'shop_coupon',
'post_status' => 'publish',
);
$coupons = get_posts( $args );
Try to get using custom post type or directly mysql query.
1) Using mysql query
// Run a query on the postmeta table to get the id of every coupon that has the email in the customer_email restrictions
$couponlist = $wpdb->get_results("SELECT
`wp_postmeta`.`post_id`
FROM `wp_postmeta`
WHERE `wp_postmeta`.`meta_key` LIKE 'customer_email'
AND `wp_postmeta`.`meta_value` LIKE '%".$email."%'");
$couponarrayfinal = array( ); //Create an array of the ids so we can use wp_query to more quickly grab the data
// Add the ids to the array in a foreach loop
foreach( $couponlist as $key => $row) {
$value = $row->post_id;
$couponarrayfinal[] = $value ;
}
2) Using get_posts method
$arg = array(
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'asc',
'post_type' => 'shop_coupon',
'post_status' => 'publish');
$coupons_list = get_posts( $arg );
Try with this you will get the all the data
// WP_Query arguments
$args = array(
'post_type' => array('shop_coupon'),
'post_status' => array('publish'),
'posts_per_page' => '-1',
'order' => 'DESC',
'orderby' => 'id',
);
// The Query
$query = new WP_Query($args);
// The Loop
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
// do something
$coupon = new WC_Coupon(get_the_ID());
$coupnCode = $coupon->code;
$coupnAmount = $coupon->amount;
$minAmount = wc_format_decimal($coupon->minimum_amount, 2);
$maximumAmount = wc_format_decimal($coupon->maximum_amount, 2);
$expire = $coupon->expiry_date;
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
I have two post types I want to display, Posts and then a Custom Post Type called 'Notes'. I want to query both of these and display them together. I've currently got it working using array_merge.
I want to create a new query so I can choose how many posts to display per page and also get pagination working. I've tried various different things to limit the amount of posts displayed but can't seem to crack it.
Here is my code:
$q1_args = array(
'post_type' => 'post',
'posts_per_page' => -1,
'post_status' => 'publish'
);
$q1_posts = get_posts( $q1_args );
// get the posts for the second query
$q2_args = array(
'post_type' => 'notes',
'posts_per_page' => -1,
'post_status' => 'publish'
);
$q2_posts= get_posts( $q2_args );
// Merge the post arrays together, and sort by date using the order_by_date function
$final_posts = array_merge( $q1_posts, $q2_posts );
usort( $final_posts, 'order_by_date' );
// Loop over the posts and use setup_postdata to format for template tag usage
foreach ( $final_posts as $key => $post ) {
$post_type = $post->post_type;
setup_postdata( $post );
//DO STUFF
}
Any thoughts on how I can limit posts per page and get pagination working?
Is there any particular reason this can't be done like this?
$args=array(
'post_type' => array('post', 'notes'),
'posts_per_page' => 15, //or any other number you want per page
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
'paged' => (( get_query_var('page') ) ? get_query_var('page') : 1)
);
$posts=get_posts($args);
if ($posts->have_posts())
{
while ($posts->have_posts())
{
$posts->the_post();
//DO STUFF
}
//add pagination here
}
else
{
// no posts found
}
wp_reset_postdata();