The wp_query below does not work when using the post_parent operator.
If I remove this option the query runs but when I add back in, it does not.
I have identified the post parent id from the category URL in the admin and it's definitely correct with 24 posts in that category.
the url for the category is wp-admin/term.php?taxonomy=category&tag_ID=2893&post_type=post&wp_http_referer=%2Fwp-admin%2Fedit-tags.php%3Ftaxonomy%3Dcategory
<?php
// WP_Query arguments
$args = array(
'post_parent' => '2893',
'post_type' => 'post',
//'post_status' => array( 'publish' ),
//'nopaging' => true,
// 'order' => 'ASC',
// 'orderby' => 'title'
);
// The Query
$sizes = new WP_Query( $args );
// The Loop
if ( $sizes->have_posts() ) {
while ( $sizes->have_posts() ) {
$sizes->the_title();
}
} else {
echo 'nothing here...';
}
// Restore original Post Data
wp_reset_postdata(); ?>
If you want to list pages that are tagged with a category term (by id) you will have to use 'category__in' => array() instead.
The post_parent argument is for getting pages ( or CPTs that are hierarchical ) where the ID passed is the page/post which is set as parent to other pages.
Example usage of getting posts tagged with category id:
$args = array(
'category__in' => array($cat_id_1, $cat_id_2) // Where $cat_id_x is an integer of the category ID (2893 in your case).
);
Pass the argument as an integer, not a string ;)
<?php
// WP_Query arguments.
$args = array(
'post_parent' => 2893, // This should be integer.
'post_type' => 'post',
// 'post_status' => array( 'publish' ),
// 'nopaging' => true,
// 'order' => 'ASC',
// 'orderby' => 'title',
);
// The Query.
$sizes = new WP_Query( $args );
// The Loop.
if ( $sizes->have_posts() ) {
while ( $sizes->have_posts() ) {
$sizes->the_title();
}
} else {
echo 'nothing here...';
}
// Restore original Post Data.
wp_reset_postdata();
Related
I've implemented an infinite scroll and in search results upon ordering by price or any custom value it doesn't work.
Here inside my enqueued script:-
isset($_GET['orderby'])?$ga_order_by = $_GET['orderby']: $ga_order_by = '';//grabbing the orderby value
if( gettype($result) == 'object') {
$ga_wp_query = new \WP_Query([ 'post_type'=> ['product_variation', 'product'], 'post__in' => $includes, 'orderby' => ['post__in',$ga_order_by], 'order' => 'ASC' ]);//so i'm ordering by search results and dynamically grabbed value.
} else {
$ga_wp_query = new \WP_Query([ 'post_type'=> 'product', 'post__in' => $includes, 'orderby' => ['post__in',$ga_order_by], 'order' => 'ASC']);
}
$args['ga_search_posts'] = json_encode($ga_wp_query->query_vars);
Inside my ajax handling function call upon search:-
$search_query = json_decode( stripslashes( $_POST['search_posts'] ), true );//this is the $args['ga_search_posts'] i'm posting via my javascript
$search_query['post_status'] = 'publish';
$search_query['posts_per_page'] = get_option('posts_per_page');
$search_query['paged'] = $_POST['page'] + 1;
wc_set_loop_prop( 'total', $_POST['search_count'] );
add_filter( 'woocommerce_get_price_html', 'labtag_show_price' );
ob_start();
query_posts( $search_query);
if ( have_posts() ) {//product loop
if ( wc_get_loop_prop( 'total' ) ) {
while ( have_posts() ) {
the_post();
wc_get_template_part( 'content', 'product' );
}
}
}
$data = ob_get_clean();
die($data);
exit;
This works except if I try to order by any parameter say price etc. Can't 'orderby' => ['post__in',$ga_order_by] declared like an array?If not should I be passing all my posts ids to the ajax handler iterate them and sort them (if this is the case, how to handle my custom order_by params)?
https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
So, with WordPress' OrderBy, you have a couple of different options.
If you want both parameters to be sorted in the same direction of ASC or DESC, then the argument anticipates a single string, with the parameters separated by a space.
Multiple 'orderby' values Display pages ordered by 'title' and
'menu_order'. (title is dominant):
$args = array(
'post_type' => 'page',
'orderby' => 'title menu_order',
'order' => 'ASC',
);
$query = new WP_Query( $args );
You use an array when you are sorting each parameter differently:
Multiple 'orderby' values using an array
> Display pages ordered by 'title' and 'menu_order' with different sort
> orders (ASC/DESC) (available since Version 4.0):
$args = array(
'orderby' => array( 'title' => 'DESC', 'menu_order' => 'ASC' )
);
$query = new WP_Query( $args );
In your case, since you are using a variable, consider building the string and then using this within your arguments array, i.e.:
//start with a space, then .= to concatenate the $_GET parameter with the space if it's set, or clear the string if it's not.
$ga_order_by = " ";
isset($_GET['orderby'])?$ga_order_by .= $_GET['orderby']: $ga_order_by = '';
//grabbing the orderby value and building our complete string.
$orderBy = 'post__in'.$ga_order_by;
if (gettype($result) == 'object') {
$ga_wp_query = new \WP_Query([ 'post_type'=> ['product_variation', 'product'], 'post__in' => $includes, 'orderby' => $orderBy , 'order' => 'ASC' ]);//so i'm ordering by search results and dynamically grabbed value.
} else {
$ga_wp_query = new \WP_Query([ 'post_type'=> 'product', 'post__in' => $includes, 'orderby' => $orderBy, 'order' => 'ASC']);
}
I have a PHP script here I can't seem to wrap my head around, im basically trying to create a shortcode that displays the next custom post type url, then when the current post is also the last post, it loops back pulling the url to the first post.
everything works except for when you are on the final post, the shortcode displays the ID of the current post (final post) instead of the url of the first post, I am assuming it has something to do with " 'fields' => 'ids' " but i have no idea as I don't write php:
function next_post_url() {
$next_post = get_next_post();
if(!$next_post){
$args = array(
'numberposts' => 1,
'post_type' => 'portfolio',
'order' => 'ASC',
'fields' => 'ids'
);
$first_post = get_posts( $args );
$next_post_url = $first_post[0];
}
else{
$next_post_url = get_next_post()->post_title;
}
return get_permalink($next_post->ID);}add_shortcode( 'next_post_url', 'next_post_url' );
Here you go please modify your php code as shown below.
function next_post_url() {
$next_post = get_next_post();
if (!$next_post) {
$args = array(
'numberposts' => 1,
'post_type' => 'portfolio',
'order' => 'ASC',
'fields' => 'ids'
);
$first_post = get_posts($args);
$post = $first_post[0];
} else {
$post = $next_post;
}
return get_permalink($post);
}
add_shortcode('next_post_url', 'next_post_url');
I have a custom post type of products, and each product belongs to a product category. I am trying to query the products to get all products sorted alphabetically by their product category i.e. First: Product: Lion
Category: Animals
Last:
Product: Snowmobile
Category: Winter
I am using a custom field for the product category, but my query doesn't sort them alphabetically - but instead by which date they are published. The product_cat_meta field is a regular text field set up in custom fields. Query is here:
function get_products()
{
$args = array(
'post_type' => 'products',
'post_status' => 'publish',
'meta_key' => 'product_cat_meta',
'orderby' => 'meta_value',
'order' => 'ASC',
'posts_per_page' => -1
);
$products = new WP_Query($args);
if ($products->have_posts()) {
$index = 0;
while ($products->have_posts()) {
$products->the_post();
$prod_meta = get_field('product_cat_meta');
echo $prod_meta;
);
$index++;
} // end while
} // end if
}
The result of this query just returns the prod category in the way they are set up in wordpress – the latest posts first, but not sorted alphabetically
You can try to put the code in functions.php file.
The code, which can be dropped into your current theme’s functions.php if you like:
function get_products()
{
$args = array(
'post_type' => 'products',
'post_status' => 'publish',
'meta_key' => 'product_cat_meta',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => -1
);
$products = new WP_Query($args);
if ($products->have_posts()) {
$index = 0;
while ($products->have_posts()) {
$products->the_post();
$prod_meta = get_field('product_cat_meta');
echo $prod_meta;
);
$index++;
} // end while
} // end if
}
Get More details, follow the link: https://codex.wordpress.org/Alphabetizing_Posts
I'm working on a short code for loop through parents ID and display all child page, but I'm not quite sure how to make the loop and make it more custom.
Here's my code:
add_shortcode( 'home-page-listing', 'get_list' );
function get_list( $atts ) {
ob_start();
$atts = shortcode_atts( array(
'ids' => ''
), $atts );
if($atts['ids']!='')
{
$id_array = explode(',',$atts['ids']);
$homePages = new WP_Query( array(
'post_type' => 'page',
'post__in'=>$id_array,
'order' => 'ASC',
'orderby' => 'post__in',
'posts_per_page' => -1
) );
if ($homePages->have_posts()){?>
<div class="">
<?php while ( $homePages->have_posts() ) : $homePages->the_post(); ?>
//here's html template code
<?php endwhile;
wp_reset_postdata(); ?>
</div>
}
}
}
Right now I can use [home-page-listing id=1,2,3,4] to display all select page ID, but I would like to make like this:
[home-page-listing parentID=4]
loop through all child page and display to the font, instead go check all the page id to display.
Thanks!
it's simple used post_parent Arguments of WP_Query. Please check below example
$homePages = new WP_Query( array(
'post_type' => 'page',
'post_parent'=>$parentID,
'order' => 'ASC',
'orderby' => 'parent',
'posts_per_page' => -1
) );
For more information of WP_Query click here
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();