Exclude featured posts through custom query not working - wordpress

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

Related

LIKE query not returning data as expected - WordPress

I really don't know what's the issue. I think my code is OK but the output is wrong. I don't know anything about WordPress, please help me.
elseif ($_GET['search']) {
$args = array(
'post_type' => 'head_to_toe_videos',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'post_title',
'value' => $_GET['search'],
'compare' => 'LIKE',
)
),
'posts_per_page' => 12,
);
}
Your query is correct but need to execute you query like this:
$the_query = new WP_Query( $args );
$result = $the_query->get_results();
echo "<pre>"; print_r($result); exit;
Try the below code your problem will solve.
elseif ($_GET['search'] != '') {
$args = array(
'post_type' => 'head_to_toe_videos',
'post_status' => 'publish',
'meta_key' => 'post_title',
'meta_query' => array(
array(
'key' => 'post_title',
'value' => $_GET['search'],
'compare' => 'LIKE',
)
),
'posts_per_page' => 12,
);
$result = new WP_Query( $args );
}

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']
)
)
);

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.

Multiples meta_key and order by specific meta_value

I am trying to get the posts order by a meta_value that i put in a sortable list of posts, i have an extra meta_key and meta_value to give extra info to the query, but i can't make the WP_Query return the post order, do you have any idea that whats is worng?
This is my WP_QUery code:
$args = array(
'meta_query' => array(
array(
'key' => 'portada',
'value' => 'on'
), array(
'key' => 'pos',
'type' => 'numeric'
)
),
'meta_key' => 'pos',
'orderby' => 'meta_value_num');
$loop = new WP_Query($args);
i use the follow code to solve my problem:
$args = array(
'meta_query' => array(
array(
'meta_key' =>'destacados',
'meta_value' => 'on'
)
),
'orderby' => 'meta_value_num',
'category__in' => $category->cat_ID,
'meta_key' => 'pos'
);
$loop = new WP_Query($args);
Thank you all :)
Note that
'meta_query' => array(
array(
'key' =>'destacados',
'value' => 'on'
)
)
instead of
'meta_query' => array(
array(
'meta_key' =>'destacados',
'meta_value' => 'on'
)
)

Resources