I want to get some products of an post_author from DB. The problem is that get_posts is not working properly, from my debug that's a problem from the theme that I am using.
Is there any alternative to get products from DB?
What I tried:
-----1----
$related_products = get_posts( array(
'post_type' => 'product',
'author' => 19,
'post_status' => 'publish',
) );
-----2----
$product_query = new WP_Query( array(
'author' => 19,
'post_type' => 'product',
'post_status' => 'publish',
) );
$related_products = $product_query->posts;
-----3----
$args = array_merge( $wp_query->query_vars, array(
'post_type' => 'product',
'author' => 19,
) );
$GLOBALS['wp_query'] = new WP_Query();
$related_products = $GLOBALS['wp_query']->query( $args );
I get results, but random products.
Related
I am making a new WP_Query to search for a product.
This shows me the wp_post information but not the stock information.
How can I order the stock results on top and the non-stock ones last in the search.
This is my current code.
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
's' => $filter_name,
'tax_query' => $tax_query,
'posts_per_page'=> $limit
);
$list = new WP_Query( $args );
Try this code but it doesn't work
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
's' => $filter_name,
'tax_query' => $tax_query,
'orderby' => 'meta_value_num',
'order' => 'DESC',
'meta_key' => '_stock',
'posts_per_page'=> $limit
);
$list = new WP_Query( $args );
I have three posts, two which have the tag eGuide one which has the tag Article. When I click on an eGuide, I want the sidebar to display other eGuide's (up to 2) and nothing else. To do this, I have the following query:
global $post;
$args = array(
'post_type' => 'resources',
'category__in' => wp_get_post_categories($post->ID ),
'posts_per_page' => 3,
'post__not_in' => array($post->ID )
);
$relatedPosts = new WP_Query( $args );
But it's showing the article too? I've also tried:
'post__not_in' => array(get_the_ID() )
... still no luck.
global $post;
$term_list = wp_get_post_terms( $post->ID, 'your_taxonomy_here', array( "fields" => "ids", "parent" => 0 ) );
$args = array (
'post_type' => 'resources',
'posts_per_page' => 3,
'post__not_in' => array( $post->ID ),
'post_status' => 'publish',
'tax_query' => array(
array (
'taxonomy' => 'your_taxonomy_here',
'field' => 'term_id',
'terms' => $term_list[0],
),
),
);
$relatedPosts = new WP_Query( $args );
You can try this code to get related posts.
Have custom post type posttype with multiple taxonomies like tax1, tax2 and post_tag which added like this:
function add_tags_categories() {
register_taxonomy_for_object_type('post_tag', 'posttype');
}
add_action('init', 'add_tags_categories');
And now searching for any solution to get most related posts by same tags count in posts.. most likely found solution like this:
$tags = wp_get_post_terms( $post -> ID, 'post_tag', ['fields' => 'ids'] );
$args = array(
'post__not_in' => array( $post -> ID ),
'posts_per_page' => -1,
'ignore_sticky_posts' => true,
'orderby' => 'count',
'tax_query' => array(
array(
'taxonomy' => 'post_tag',
'terms' => $tags
)
)
);
$my_query = new wp_query( $args );
But its not working.. I got $tags in array with ids,
query also seems working, just not finding any similar posts... but I have created and similar, and identical with tags also.. But result is still 0 posts..
Found solution in here
so I made like this:
$tags = wp_get_post_terms( $post -> ID, 'post_tag', ['fields' => 'ids'] );
$args = array(
'post_type' => 'posttype',
'post__not_in' => array( $post -> ID ),
'posts_per_page' => -1,
'ignore_sticky_posts' => true,
'orderby' => 'tag_count',
'order' => 'DESC',
'tag__in' => $tags,
);
add_filter( 'posts_clauses', 'wpse173949_posts_clauses', 10, 2 );
$query = new WP_Query( $args );
remove_filter( 'posts_clauses', 'wpse173949_posts_clauses', 10 );
I'm trying to show products from a certain category on a page like his:
$args = array( 'post_type' => 'product', 'posts_per_page' => 5, 'product_cat' => 'prcategory1', 'orderby' => 'price');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
This one works. However, I'd like 'prcategory1' to be taken from a custom field of the page. Something like this (incorrect code incoming):
$args = array( 'post_type' => 'product', 'posts_per_page' => 5, 'product_cat' => 'get_post_meta(get_the_ID(), 'custom_cat_name', TRUE); ?>', 'orderby' => 'price');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); global $product; ?>
There is some errors in your code, try this for your array :
$args = array(
'post_type' => 'product',
'posts_per_page' => 5,
'product_cat' => get_post_meta(
get_the_ID(),
'custom_cat_name',
TRUE
),
'orderby' => 'price'
)
I am trying to fetch products by category ID this way:
<?php
$args = array(
'posts_per_page' => 20,
'taxonomy' => 'product_cat',
'post_type' => 'product',
'post_status' => 'publish',
'cat' => $cat_id
);
$query = new WP_Query($args);
$posts = get_posts($args);
var_dump($posts);
?>
The $cat_id variable contains the correct category ID. I've checked it. Products are added to correct categories.
The problem is, whenever I var_dump the $posts variable I get an empty array. As soon as I remove the 'cat' keyword from the arguments I can fetch products from all categories with no problems. The only problem is the 'cat' keyword.
Am I doing something wrong?
You could try this instead:
$args = array(
'posts_per_page' => 20,
'post_type' => 'product',
'post_status' => 'publish',
'tax_query' = array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'term' => $cat_id
)
);
$query = new WP_Query($args);
var_dump($query);
I haven't test it, but it should work.