Wordpress get Custom Post by Custom Taxonomy - wordpress

I created a custom post and Custom Taxonomy
I need to search post using taxonomy
used this code but result is null
$properties = new WP_Query(
array( 'posts_per_page' => 10,
'orderby' => 'ID',
'order' => 'DESC',
'post_type' => 'real-property',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'tenure',
'terms' => 'val',
'field' => 'slug'
)
),
)
);

I found a solution myself: issue is WP_Query() function passed valued in array
<?php
$args = array( 'posts_per_page' => 10,
'orderby' => 'ID',
'order' => 'DESC',
'post_type' => 'real-property',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'tenure',
'terms' => 'val',
'field' => 'slug'
)
),
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
the_title();
endwhile;
wp_reset_postdata();
else :
esc_html_e( 'Sorry, no posts matched.' );
endif;
?>

Related

my custom taxonomy does not work and only returns the most recent posts

I have a custom post type called cme-education which I am trying to query to get only the posts that have a term of covid-curriculum however, the tax query does not work and only returns the most recent post. See my code below. my file is called block_random-post-types.php
$args = array(
'post_type'=> 'cme-education',
// 'orderby'=>'rand',
'posts_per_page' => '2',
'post_status' => 'publish',
// 'exclude=' => $currentID,
'tax_query' => array(
'taxonomy' => 'covid-curriculum',
'field' => 'slug',
'terms' => array('covid-19'),
'operator' => 'IN'
),
);
$covid = new WP_Query($args);
while ($covid->have_posts()) : $covid->the_post();
?>
<h2><?php the_title(); ?></h2>
<?php echo "launch activiry"; ?>
<?php
endwhile;
//wp_reset_postdata();
?>
use below shared code:-
**
$args = array(
'post_type' => 'cme-education',
'posts_per_page' => '2',
'post_status' => 'publish'
'tax_query' => array(
array(
'taxonomy' => 'covid-curriculum',
'field' => 'slug',
'terms' => 'covid-19',
),
),
);
**

Woocommerce products only "in stock"

There is the following code, the problem is that it displays all the goods in a row, regardless of status, but I would like to display only those that are "in stock".
function get_products($categories = array(), $product_type = 'featured_product', $paged = 1, $post_per_page = -1, $orderby = '', $order = '') {
global $woocommerce, $wp_query;
$args = array(
'post_type' => 'product',
'posts_per_page' => $post_per_page,
'post_status' => 'publish',
'paged' => $paged,
'orderby' => $orderby,
'order' => $order
); }
I'm not really sure what you're trying to do with your code, as your function and your args seem to be unrelated... but if you're just trying to get the products, try a custom loop:
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => 'publish',
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
),
),
'meta_query' => array(
array(
'key' => '_stock_status',
'value' => 'instock',
'compare' => '=',
),
),
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) : $query->the_post();
// Do stuff.
endwhile;
endif;
wp_reset_postdata();
Note that I have not tested this and you clearly have something unique going on with how you're trying to display the posts, so you may need to make a few adjustments.

Get Products in Category WordPress

I'm trying to pull all products located within a certain category slug.
I've tried the following, however both return every product in the store.
$products = wc_get_products( array( 'category' => array( 'Sony' ) ));
and
$productlist = wp_query(array( 'post_type' => 'product', 'product_cat' => 'Sony'));
Any pointers would be much appreciated.
Try this code
$args = array(
'post_type' => 'product',
'posts_per_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'category',
'terms' => 12,// category ID
),
),
);
Try This:
<?php
$args = array( 'post_type' => 'product', 'posts_per_page' => 1, 'product_cat' => 'Sony', 'orderby' => 'rand' );
$data = new WP_Query( $args );
print_r($data);
Try with this and replace ENTER_CATEGORY with slug name or use simple 'terms' => 'slug/categoryname'
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'terms' => array_map( 'sanitize_title', explode( ',', 'ENTER_CATEGORY' ) ),
'field' => 'slug',
'operator' => $atts['operator']
)
)
);

How to query specific custom post with custom fields in a post_type?

I'm new into wordpress and kinda confused how to get 1 post in a custom post. I only knew the loop where it echo all the content in a post_type.
I'm aiming to get 1 post from a post_type 'product-category' and the meta_key is 'product-category-and-type'
You could try this query for custom in wordpress:
$args = array(
'post_type' => 'product-category',
'post_status' => 'publish',
"numberposts" => 1,
'meta_query' => array(
array(
'key' => 'product-category-and-type',
'value' => 'meta_value'
)
)
);
$getPosts = new WP_Query($args);
Just copy and paste below mentioned code at your desired position and replace "your_meta_value" with your actual meta value for meta key "product-category-and-type".
You will get your expected result :
<?php $args = array(
'post_type' => 'product-category',
"numberposts" => 1,
'post_status' => 'publish',
'meta_query' => array(array('key' => 'product-category-and-type','value' => 'your_meta_value'))
);
$myposts = new WP_Query($args);
while($myposts->have_posts()) : $myposts->the_post();
the_title();
endwhile;?>
$args = array(
'post_type' => 'product-category',
'post_status' => 'publish',
'posts_per_page' => '1',
'tax_query' => array(
array(
'taxonomy' => 'product-category-and-type',
'field' => 'slug'
),
),
);
$result = new WP_Query($args);

Filter tax_query in my wordpress

I would like to know how i filters property area , i Try :
<?php
$args = array(
'post_type' => 'estate_property',
'post_status' => 'publish',
'tax_query' => array(
'taxonomy' => 'property_area',
'field' => 'slug',
'terms' => 'pigalle',
),
);
$selection = new WP_Query($args);
?>
But all show ! WHY ? lol
Thanks
Read up on Taxonomy Parameters
The tax_query parameter is a multi-dimensional array. You need to wrap it with another array:
<?php
$args = array(
'post_type' => 'estate_property',
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'property_area',
'field' => 'slug',
'terms' => 'pigalle'
)
)
);
$selection = new WP_Query($args);
?>

Resources