How to exclude a post that has empty custom field (for example "slides") from the wordpress loop?
You use meta_query in your Wp_query like this:
In this example they are only showing posts where the custom field "color" doesn't have the value "blue"
$args = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => 'color',
'value' => 'blue',
'compare' => 'NOT LIKE'
)
)
);
$query = new WP_Query( $args );
See this page: http://codex.wordpress.org/Class_Reference/WP_Query and scroll to the "Custom Field Parameters" section.
Related
I am trying to get specific product using meta key value. Any help would be highly appreciated.
I am trying to display a specific product when the product code is passed via a shortcode.
I have setup metabox and confirmed that the items have custom meta values with key "neproductinfo-ne_item_code"
$atts = shortcode_atts(
array(
'itemcode' => '',
),
$atts, 'products_catalog'
);
$woocommerce_loop['columns'] = 1;
$meta_query_args = array(
array(
'key' => 'neproductinfo-ne_item_code',
'value' => $atts['itemcode'],
'compare' => '='
)
);
$meta_query = new WP_Meta_Query( $meta_query_args );
$products = new WP_Query( array (
'post_type' => 'product',
'post_status' => 'publish',
//'posts_per_page' => 1,
'meta_query' => $meta_query
));
I am getting list of all products regardless of any itemcode passed through shortcode
shortcode example
[products_catalog itemcode='1001']
No if condition found where you are checking the attribute value exists or not.
Based on existence of attribute value, you need to pass meta query. If there is no value then y you passs the meta_query to WP_Query ?
I have a custom post type of post_type_1 in WordPress, I also have a custom field in that post type of custom_field_data
I am doing a search for apples by querying posts using wp_query like this...
$search_term = 'apples';
$args = array(
'post_type' => array('post_type_1'),
'post_status' => array('publish'),
'posts_per_page' => -1,
's' => sanitize_text_field( $search_term)
);
$results= new WP_Query( $args );
This works correctly and returns all posts with apples in the title, but I would also like to extend the search to the custom field custom_field_data so the query will return all posts with apples in either title or custom field.
What is my best approach? I have tried using a meta_query but haven't been successful. Does anybody have an example?
Use below code will work for custom field search.
$custom_field = $_GET['custom_field '] != '' ? $_GET['custom_field '] : '';
$search_term = 'apples';
$args = array(
'post_type' => array('post_type_1'),
'post_status' => array('publish'),
'posts_per_page' => -1,
's' => sanitize_text_field( $search_term),
'meta_query' => array(
array(
'key' => 'custom_field_key',
'value' => $custom_field ,
'compare' => 'LIKE',
),
)
);
$results= new WP_Query( $args );
Tested and works well.
I'm trying to link a custom field to a custom shortcode so the shortcode shows displays only the posts with the custom field selected to it.
This is my code below for my shortcode as you can see the key is my custom felid "flash_deal". When I enter the shortcode I just get all the perk psots and no the custom field perk posts?
add_shortcode('foundry_flash', 'shortcode_query_flash');
function shortcode_query_flash($atts, $content){
extract(shortcode_atts(array( // a few default values
'post_type' => 'perks',
'posts_per_page' => -1 ,
'meta_query' => array(
array(
'key' => 'flash_deal', // name of custom field
'value' => '"yes"', // matches exactly "red"
'compare' => 'LIKE'
)
)
), $atts));
The code you show is simply setting up variables - not running any queries at all (see extract documentation & shortcode_atts documentation).
I assume you have more code you just didn't add here, so likely the problem is the double quotes around yes that are causing issues. It's literally looking for "quoteyesquote".
As a general working example to get "Parks" Post based on custom meta, you need to use WP_Query as in the docs here.
$args = array(
'post_type' => 'parks',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'flash_deal',
'value' => 'yes',
'compare' => 'LIKE',
),
),
);
$query = new WP_Query( $args );
Then use a Nested Loop setup to loop through the results.
I have built a search a form at custom page in wordpress and want to filter product on shop page using meta keys that are already exists into posts table.
Initially I have tried to filter categories at form page like this but it doesn't work.
$meta_query = array(
'key' => '_years',
'value' => '2009'
);
$args=array(
'meta_query' => $meta_query,
'posts_per_page' => 10,
'post_type' => 'product',
'orderby' => $orderby,
'order' => $order,
'paged' => $paged
);
wc_product_dropdown_categories($args);
The meta_query parameter must be change in an array of array for a single custom field handling:
$meta_query = array(
array(
'key' => '_year',
'value' => '2009',
'compare' => '>',
)
);
Some details from WP_Query page in the Single Custom field handling part
Hope it will work with this.
I use custom fields for lots of my content on both Posts and Pages, so that half of the text will be in the main post body and the other half in a custom field.
Unfortunately, when doing this only the text in the post body is searchable.
I want to have all the custom fields included in the search (just joined together as one). Preferably without installing a plugin, as all the plugins I have seen do not seem to be able to do this or they add loads of options I don't need.
I just would like one search box, that uses WordPress's default search but finds words in custom fields as well as those in the main body.
Is this possible?
you can do this by meta_query:
Try something like
$args = array(
'post_type' => 'custom_post',
'posts_per_page' => 10,
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'custom_filed1',
'value' => $_REQUEST['custom_filed1'],
'compare' => 'LIKE'
),
array(
'key' => 'custom_field2',
'value' => array( $_REQUEST['custom_field2'], $_REQUEST['custom_field2'] ),
'type' => 'numeric',
'compare' => 'BETWEEN'
)
);
$wp_query = new WP_Query( $args );
//echo $wp_query->request;
while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
//DISPLAY POST HERE
may this help you