I got an array of Post Ids
$ids = array(1277,6098,6709, 6098);
I want to loop throw these specific posts with:
$args = array( 'orderby' => 'post__in',
'post__in' => $ids);
get_posts($args);
$custom_posts = get_posts($args);
foreach( $custom_posts as $post ) : setup_postdata($post);
the_title();
...
endforeach;
But wordpress automatically excludes the repeated Id (6098). How can I avoid this?
I tryed to create my own function. But unfortunately it doesnt work. I created my own get_posts function like this:
function get_posts_jt($args = null) {
$defaults = array(
'numberposts' => 5, 'offset' => 0,
'category' => 0, 'orderby' => 'post_date',
'order' => 'DESC', 'include' => array(),
'exclude' => array(), 'meta_key' => '',
'meta_value' =>'', 'post_type' => 'post',
'suppress_filters' => true
);
$r = wp_parse_args( $args, $defaults );
if ( empty( $r['post_status'] ) )
$r['post_status'] = ( 'attachment' == $r['post_type'] ) ? 'inherit' : 'publish';
if ( ! empty($r['numberposts']) && empty($r['posts_per_page']) )
$r['posts_per_page'] = $r['numberposts'];
if ( ! empty($r['category']) )
$r['cat'] = $r['category'];
if ( ! empty($r['include']) ) {
$incposts = $r['include'];
$r['posts_per_page'] = count($incposts); // only the number of posts included
$r['post__in'] = $incposts;
} elseif ( ! empty($r['exclude']) )
$r['post__not_in'] = wp_parse_id_list( $r['exclude'] );
$r['ignore_sticky_posts'] = true;
$r['no_found_rows'] = true;
$get_posts = new WP_Query;
return $get_posts->query($r);
}
I changed the line from:
$incposts = wp_parse_id_list( $r['include'] );
to:
$incposts = $r['include'];
to avoid removing duplicated Ids from array. But this function still doesnt show the duplicated posts from my Id List.
Any Ideas?
You can loop your ids and call get_post with setup_postdata :
global $post;
foreach ($ids as $id) :
$post = get_post($id);
setup_postdata( $post );
the_title();
endforeach;
You can't: get_posts uses wp_parse_id_list to remove duplicate ids from the supplied array.
Related
I'm trying to get the descritpiton of the parent product from a series of variation.
I tried this way
$args = array(
'numberposts' => -1,
'post_type' => 'product_variation',
'suppress_filters' => false,
'orderby' => 'title',
'order' => 'DESC' );
$products = get_posts( $args );
foreach ( $products as $productId ) {
$product = wc_get_product( $productId );
$parent = $product->get_parent_data();
....
where product is my variation. But in parent data i haven't a description.
Any suggestions on how can I get the description?
You can use get_parent_id to get the parent product id. Try the below code.
$args = array(
'numberposts' => -1,
'post_type' => 'product_variation',
'suppress_filters' => false,
'orderby' => 'title',
'order' => 'DESC'
);
$products = get_posts( $args );
$productsIds = array(); // store product id for avoid duplication.
foreach ( $products as $variation_id ) {
$variation = wc_get_product($variation_id);
$product = wc_get_product( $variation->get_parent_id() );
if( !in_array( $product->get_id(), $productsIds ) && $product ){
$productsIds[] = $product->get_id();
$product_details = $product->get_data();
$product_full_description = $product_details['description'];
$product_short_description = $product_details['short_description'];
}
}
I have a custom post type 'product' that returns quite a lot of data in the API response, up to 400 posts with a lot of nodes. Almost all the data is coming from advanced custom fields (I'm using ACF to API plugin to expose it).
On the 'products' page, I only need to show the title & image of the product. Is there a way to remove all other fields when requesting all products with https://example.com/wp-json/wp/v2/product, but leave that data in place when requesting a specific product with https://example.com/wp-json/wp/v2/product/123 ?
You better create a custom endpoint for all products. Add the code below in your custom plugin or add it in functions.php of theme (I will recommend the custom plugin approach though)
You can then access it using https://example.com/wp-json/custom/v1/all-products
add_action( 'rest_api_init', 'rest_api_get_all_products' );
function rest_api_get_all_products() {
register_rest_route( 'custom/v1', '/all-products', array(
'methods' => WP_REST_Server::READABLE,
'callback' => 'rest_api_get_all_products_callback',
'args' => array(
'page' => array(
'sanitize_callback' => 'absint'
),
'posts_per_page' => array(
'sanitize_callback' => 'absint'
)
)
));
}
function rest_api_get_all_products_callback( $request ) {
$posts_data = array();
$paged = $request->get_param( 'page' );
$posts_per_page = $request->get_param( 'posts_per_page' );
$paged = ( isset( $paged ) || ! ( empty( $paged ) ) ) ? $paged : 1;
$posts_per_page = ( isset( $posts_per_page ) || ! ( empty( $posts_per_page ) ) ) ? $posts_per_page : 10;
$query = new WP_Query( array(
'paged' => $paged,
'posts_per_page' => $posts_per_page,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
'post_type' => array( 'product' )
)
);
$posts = $query->posts;
if( empty( $posts ) ){
return new WP_Error( 'no_post_found', 'No Products Found.', array( 'status' => 404 ) );
}
foreach( $posts as $post ) {
$id = $post->ID;
$post_thumbnail = ( has_post_thumbnail( $id ) ) ? get_the_post_thumbnail_url( $id ) : null;
$posts_data[] = (object) array(
'id' => intval($id),
'title' => $post->post_title,
'featured_img' => $post_thumbnail
);
}
$response = rest_ensure_response( $posts_data );
return $response;
}
I have a recipe website and search option in it. Now i have recipe name CAKE and when i search CAKES then its not showing result. Its fine with CAKE or CAK . I want result with LIKE query. Please help me out.
Here is my custom search args code:
<?php $args = array( 'posts_per_page' => -1, 'post_type' => 'recipes', 's' => $_GET['s'] );
$myposts = get_posts( $args );
if ( empty( $myposts ) ) {
echo 'No results found in Recipe!';
} else { ?>
//show recipes
<?php wp_reset_postdata();
} ?>
Try this
$args = array('posts_per_page' => -1, 'post_type' => 'recipes','s' => $_GET['s']);
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
//whatever you want to do with each post
}
} else {
// no posts found
}
After so many research i found an alternate. I change plural terms into singular with some php manipulation. Here is i am sharing it.
$str = $_GET['s'];
$result = rtrim($str, 's');
$result_sing = str_pad($result, strlen($str) - 1, 's');
$args = array(
'posts_per_page' => -1,
'post_type' => 'recipes',
's' => $result_sing,
);
I made a file (test.php) in my WordPress root folder, and load it separately from WordPress like this http://localhost/MyWP_Website/test.php
Here are my code:
<?php
require_once("wp-config.php");
require_once("wp-includes/wp-db.php");
function myFilter001($where = '') {
GLOBAL $wpdb;
$where .= " AND ".$wpdb->posts.".ID > 20 ";
return $where;
}
add_filter('posts_where', 'myFilter001');
$args = array(
'posts_per_page' => $total,
'category' => $category,
'author' => $author,
's' => $search,
'offset' => 0,
'orderby' => 'ID',
'order' => 'DESC',
);
$posts = get_posts( $args );
?>
I searched and find that my function is not public but how to make it public? it's with my main code and in my only file that I need?!
I found the solution!
By using WP_Query() instead of get_posts() all filters added to posts_where will working, I just replaced last line of my code.
Replace this:
$posts = get_posts( $args );
To this:
$query = new WP_Query( $args );
I want to find all posts from same categories and same tags like a specific post in a plugin. So I do it actually separately by querying for tags and categories:
$taxonomy_arr = wp_get_post_tags( $this->post->ID, array( "fields" => "ids" ) );
add_filter( 'posts_where', array( $this, 'additional_filter' ) );
foreach ( $taxonomy_arr as $tag_id ) {
$posts_arr = get_posts( array(
'posts_per_page' => $this->max_results,
'tag_id' => (int) $tag_id,
'post_type' => array( $this->included_post_types ),
'orderby' => 'rand',
'suppress_filters' => false
) );
// add to categories selection
if ( is_array( $posts_arr ) ) {
foreach ( $posts_arr as $post_obj ) {
if ( is_object( $post_obj ) ) {
$local_taxonomy_selection[] = (int) $post_obj->ID;
}
}
}
}
// get post categories
$category_array = get_the_category( $this->post->ID );
foreach ( $category_array as $category ) {
$posts_arr = get_posts( array(
'posts_per_page' => $this->max_results*2,
'category' => $category->cat_ID,
'post_type' => array( $this->included_post_types ),
'orderby' => 'rand',
'suppress_filters' => false
));
// add to categories selection
if ( is_array( $posts_arr ) ) {
foreach ( $posts_arr as $post_obj ) {
if ( is_object( $post_obj ) ) {
$local_category_selection[] = (int) $post_obj->ID;
}
}
}
}
// combine post id's arrays
$all_posts = $local_taxonomy_selection + $local_category_selection;
It's working but is there a way to do it in one query?
This question already has answer on wordpress.stackexchange. So copying same code here.
https://wordpress.stackexchange.com/questions/4201/how-to-query-posts-by-category-and-tag
global $wp_query;
$args = array(
'category__and' => 'category',
'tag__in' => 'post_tag', //must use tag id for this field
'posts_per_page' => -1
$posts = get_posts($args);
foreach ($posts as $post) :
//do stuff
endforeach;