Woocommerce products only "in stock" - wordpress

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.

Related

Exclude featured posts through custom query not working

I am trying to exclude posts from query and it is not working at all.
Here what I tried
<?php
$args = array(
'post_type' => 'videos-presentations',
'post_status' => 'publish',
'posts_per_page' => 4,
'paged' => $paged,
'meta_query' => array(
array(
'meta_key' => '_is_ns_featured_post',
'meta_value' => 'yes',
'meta_compare' => '!='
)
)
);
$my_query = new WP_Query($args);
?>
Also Tried with
'meta_compare' => 'NOT EXIST'
and
'meta_compare' => 'NOT IN'
Any idea what I am doing wrong?
Got it. From here
It Works with just
'meta_query' => array(
array(
'key' => '_is_ns_featured_post',
'compare' => 'NOT EXISTS'
)
)
function exclude_posts ( $query ) {
$meta_query = $query->get( 'meta_query' );
$meta_query[] = array(
'key'=>'_is_ns_featured_post',
'value'=>'yes',
'compare'=>'!=',
);
$query->set( 'meta_query',$meta_query );
}
add_action( 'pre_get_posts', 'exclude_posts' );
place this code in functions.php file of active theme

Wordpress get Custom Post by Custom Taxonomy

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;
?>

Wordpress get only posts with matching title not content

Currently with this code below, I get posts whose title or description contains search keyword. How do I change it, so I only get posts whose title contains search keyword.
$search_keyword = esc_attr( $_REQUEST['query'] );
$ordering_args = $woocommerce->query->get_catalog_ordering_args( 'title', 'asc' );
$suggestions = array();
$args = array(
's' => apply_filters( 'yith_wcas_ajax_search_products_search_query', $search_keyword ),
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'orderby' => $ordering_args['orderby'],
'order' => $ordering_args['order'],
'posts_per_page' => apply_filters( 'yith_wcas_ajax_search_products_posts_per_page', get_option( 'yith_wcas_posts_per_page' ) ),
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array( 'search', 'visible' ),
'compare' => 'IN'
)
)
);
if ( isset( $_REQUEST['product_cat'] ) ) {
$args['tax_query'] = array(
'relation' => 'AND',
array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => $_REQUEST['product_cat']
) );
}
$products = get_posts( $args );
I was trying to add one more query but only make it worse.

Pagnation wordpress custom post type category page

Can't get the pagnation to work on the custom post types category page. It works when displaying the custom archive page. When I click on the pagnation it shows the posts from the first page but the URL says page=2.
This is the code i'm using in the archive-slug.php. How can I customize it to work with the taxonomy-slug.php?
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
query_posts( array( 'post_type' => 'dropshippers', 'paged' => $paged ) );
$loop = new WP_Query( array( 'post_type' => 'dropshippers', 'paged' => get_query_var( 'paged' ), 'posts_per_page' => 8 ) );
if(have_posts()) : while(have_posts()) : the_post();
//Posts
endwhile; endif;
if(function_exists('wp_pagenavi')) {
wp_pagenavi( array( 'query' => $loop ) );
} else {
echo "No posts";
}
You need to add the name of you category in your query, i prefer to use get_posts for that:
<?php $args = array(
'posts_per_page' => 8,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'dropshippers',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
$posts_array = get_posts( $args ); ?>
Fullfit this two line with your args:
'category' => '',
'category_name' => '',

Adding Pagination to Wordpress query for categories

I am trying to add pagination to a custom category list I have for a website gallery using WordPress. I have tried to follow a few guides but unable to add the paging function to my query. I have added my current queries.
Any help would be appreciated.
<?php
global $paged;
$curpage = $page ? $paged :1;
$args2 = array(
'taxonomy' => 'media-category',
//'parent' => '5',
'child_of' => 7,
'orderby' => 'date',
'order' => 'ASC',
'hide_empty' => 0,
'number' => 99,
'posts_per_page' => 3,
'paged' => $paged,
);
?>
<ul class="gallery-list small-block-grid-1 medium-block-grid-2 large-block-grid-3">
<?php
$categories = get_categories($args2);
foreach ($categories as $category) {
$args = array(
'post_type' => 'attachment',
'post_status' => 'any',
'posts_per_archive_page' => -1,
'tax_query' => array(
array(
'taxonomy' => 'media-category',
'field' => 'slug',
'terms' => array($category->name, 'featured'),
'operator' => 'AND',
),
),
);
$custom_query = new WP_Query($args);
?>
<?php while ($custom_query->have_posts()) : $custom_query->the_post(); ?>
I found the answer.
// Sets manual pagination
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$posts_per_page = 12;
$offset = ($posts_per_page * $paged) - 12 ;
$args = array(
'orderby' => 'id',
'order' => 'DESC',
'hide_empty' => 0,
'number' => $posts_per_page,
'offset' => $offset,
);
$categories = get_terms($taxonomies, $args);
This sets the page up to have 12 items per page and offsets the page number.

Resources