Wordpress Query based on Checkbox Value - wordpress

I'm trying to query my custom post type of "projects" and return all posts that have the "custom_featured" checkbox checked on. This is my current query, however it's not returning anything although I have several posts with that checkbox checked.
$args = array(
'post_type' => 'projects',
'meta_query' => array(
array(
'key' => 'custom_featured',
'value' => 'true',
'compare' => '='
)
)
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<a href="<?php the_permalink();?>">
<h1><?php the_title(); ?> </h1>
</a>
<?php endwhile;
}
wp_reset_query();

I figured this out. The 'value' should be "on" and not "true"

Use the following code, if you are saving text values in the database from checkbox
'meta_query' => array(
array(
'key' => 'custom_field_key',
'value' =>'custom_field_value',
'compare' => 'LIKE'
)

Related

WP_Query - posts_per_page parameter not working?

So the setup is working fine, however I tried to limit the number of titles shown but nothing is working. What am I missing? So it should be limited to 10 or 5 titles.
I tried everything I could think of but for some reason it is not taking the limit in account.
<?php
$meta_query = array();
$args_booking = array(
'post_type' => 'booking',
'post_status' => array('publish', 'pending', 'canceled'),
'nopaging' => 'false',
'posts_per_page' => 1,
'orderby' => array(
'menu_order' => 'ASC',
'date' => 'DESC',
),
);
$meta_query[] = array(
'key' => GOLO_METABOX_PREFIX. 'booking_item_author',
'value' => $user_id,
'type' => 'NUMERIC',
'compare' => '=',
);
$args_booking['meta_query'] = array(
'relation' => 'AND',
$meta_query
);
//$data_booking = new WP_Query($args_booking);
//$total_post = $data_booking->found_posts;
if( count($results) > 0 ){//if( $total_post > 0 ){
?>
<ul class="listing-detail custom-scrollbar">
<?php foreach ($results as $r):?>
<?php
$lang = $r->lang!='nl'?'/' . $r->lang : '';
$param = http_build_query(json_decode(stripslashes($r->filter_data), true));//json_decode($val, true, JSON_UNESCAPED_SLASHES);
$url = site_url("/") . 'search-results/?' . $param . "&sid=" . $r->ID;
?>
<li><?php echo get_city($r->city_id)->name;?></li>
<?php endforeach;?>
</ul>
<?php
}else{
?>
<span class="no-item"><?php esc_html_e('No recent plan', 'golo-framework'); ?></span>
<?php
}
?>
There are a couple of filters that run before WP_Query actually executes the desired SQL command, which means you posts_per_page setting can actually get overridden before your results are returned.
Unfortunately this can be done in both the theme you may be using, and in any plugins that maybe active. What I would do in this situation, is first check any of my other code to see if I am modifying the pre_get_posts or the post_limits filter.
I'd then check the theme settings to see if it has a setting for this, and last resort would be to disable plugins one by one to see if any of those are filtering the query.
Lastly don't discount cache being an issue. Some hosts can force caching of pages, even when you are logged in to WordPress.
I corrected this like this.
So you can take a pattern.
Let me know if it does not work or does
I used posts_per_page to set the number of posts per page (number of titles per page) Or you can use numberposts.
numberposts is used to search and display only 20 posts and posts_per_page is used to display 20 posts per page.
$args_booking = array(
'post_type' => 'booking',
'post_status' => array('publish', 'pending', 'canceled'),
'nopaging' => 'false',
'posts_per_page' => 20, // for set limit.
'orderby' => array(
'menu_order' => 'ASC',
'date' => 'DESC',
),
'meta_query' => array(
array(
'key' => GOLO_METABOX_PREFIX . 'booking_item_author',
'value' => $user_id,
'type' => 'NUMERIC',
'compare' => '=',
)
)
);
$query = new WP_Query($args_booking);
$results = $query->get_posts();
if (count($results) > 0) {//if( $total_post > 0 ){
?>
<ul class="listing-detail custom-scrollbar">
<?php foreach ($results as $r): ?>
<?php
$lang = $r->lang != 'nl' ? '/' . $r->lang : '';
$param = http_build_query(json_decode(stripslashes($r->filter_data), true));//json_decode($val, true, JSON_UNESCAPED_SLASHES);
$url = site_url("/") . 'search-results/?' . $param . "&sid=" . $r->ID;
?>
<li>
<a href="<?php echo $url; ?>" class="place-view"
data-id="<?php echo $r->ID; ?>">
<?php echo get_city($r->city_id)->name; ?>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php
} else {
?>
<span class="no-item"><?php esc_html_e('No recent plan', 'golo-framework'); ?></span>
<?php
}

Wordpress display posts from multiple value in meta

I have got main product with meta key: addons and meta values in this key: 129456,968945,495435 Each of these three numbers is the key with these meta values. For example:
Post 1: meta_key: subproduct meta_value: 129456
Post 2: meta_key: subproduct meta_value: 968945
Post 3: meta_key: subproduct meta_value: 495435
And now I want to display these three posts in the main product. My code:
<?php if (!empty($addons = get_post_meta(get_the_ID(), 'addons', true))):?>
<?php
$params = array(
'post_type' => 'product',
'meta_key' => 'subproduct',
'meta_value' => $addons
);
$wc_query = new WP_Query($params);
?>
<?php while ($wc_query->have_posts()) : $wc_query->the_post(); ?>
<?php include(rh_locate_template('inc/parts/woomain.php')); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif;?>
With one meta value it worked but with several it doesn't work anymore. How do you view these three posts?
Try to alter your query like this and lets see if that works
$params = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => 'subproduct',
'value' => array($addons),
'compare' => 'IN'
)
)
);
It works with this code:
$params = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => 'subproduct',
'value' => $addons,
'compare' => 'IN'
)
)
);

Select post with specific group - subfield value

in wordpress I have custom post type 'referenzen'. This post type has Custom fields(ACF) 'Referenzen-buildin-type' Group with subfield 'building-type' which is checkbox. I do not know how to select posts with specific building type. This is not working for me :
$posts = get_posts(array(
'meta_query' => array(
array(
'key' => 'referenzen-building-types_building-type',
'value' => '"Museen"',
'compare' => 'LIKE'
)
)
));
Any idea? Thanks
Please have a look below:
<?php
//args
$args = = array (
'numberposts' => -1,
'post_type' => 'event',
'meta_key' => 'location',
'meta_value' => 'Melbourne'
);
//query
$the_query = new WP_Query( $args );?>
<?php if( $the_query->have_posts() ): ?>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
/* do something */
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); // Restore global post data stomped by the_post(). ?>
I hope it would help you out.
$posts = get_posts(array(
'meta_query' => array(
array(
'key' => 'referenzen_building_types_building_type',
'value' => 'Museen',
'compare' => 'LIKE'
)
)
));

Wordpress: Iterative Parent Page if Custom Field Contains Page ID

I am trying to display a list based on the page. The list is built based on data in an ACF custom field, which is in a serialized array.
The list is for employees in a department. I am setting the parent page in Wordpress but want the employees to be listed on all child page. Employees may also need to be listed in multiple departments, so the ACF field is stored as a serialized array.
I have been able to get this to work if the current page is in the array, but am having problems when the data is for a parent, grandparent, etc. page. How can I iterate through this to find the first parent page that matches the criteria?
Relevant code (additional content is displayed, some included in case it is helpful in your answer):
$postid = get_the_ID();
$staffList = get_posts(array(
'post_type' => 'staff',
'meta_key' => 'position_order',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'ministries', // name of custom field
'value' => $postid,
'compare' => 'LIKE',
),
array(
'key' => 'administration', // name of custom field
'value' => $postid,
'compare' => 'LIKE',
)
)
));
if(( $staffList ) != '') {
foreach ( $staffList as $staff ) : setup_postdata( $people );
$pid = $staff->ID;
$featured_img_url = get_the_post_thumbnail_url($pid, 'full');
$highres = get_field('high_res', $pid);
$normal = get_field('normal', $pid);
$biofile = get_field('bio_file', $pid);
$scontent = $staff->post_content;
$contact = apply_filters('the_content',$scontent);
?>
<div class="profile">
<div class="text-center">
<img src="<?php echo $featured_img_url; ?>" width="130"
alt="<?php echo $staff->post_title; ?>">
</div>
<div class="profile__name"><?php echo $staff->post_title; ?></div>
<?php echo $contact; ?>
<?php if ( $biofile != "" ) { ?>
<span class="icon-file"></span> <?php echo $people->post_title; ?> Bio
<?php } ?>
</div>
<?php
endforeach;
wp_reset_postdata();
?>
<?php } endif; ?>
I'm thinking the query needs to be placed in a function, but having a problem getting it to run correctly and then iterate through parent posts until it finds a results. The concept is:
if no employees for current $pageid, then get $parent_pageid and run query. if no employees found, find grandparent_pageid and run query, etc., until either employees are found or there are no more parent pages.
I've found this: Recursive function to get the parent caegory description that seems like a good way to iterate, just having a problem modifying for my needs.
Thanks for your help!
This function will loop through parent posts until it returns results:
function get_staff_posts($post) {
$staffList = new WP_Query( array(
'post_type' => 'staff',
'meta_key' => 'position_order',
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'ministries', // name of custom field
'value' => $post->ID,
'compare' => 'LIKE',
),
array(
'key' => 'administration', // name of custom field
'value' => $post->ID,
'compare' => 'LIKE',
),
),
));
$parent = $post->post_parent;
if( !$staffList->have_posts() && $parent != 0 )
get_staff_posts($parent);
return $staffList;
}
The function will return the query, so you could use it in your theme like this:
$staffList = get_staff_posts($post);
if( $staffList->have_posts() ):
while( $staffList->have_posts() ): $staffList->the_post();
// output
endwhile; wp_reset_postdata();
endif;

WP magic fields query_posts

I'm trying to display titles from posts of type 'products' with that have a custom field "product_category" (dropdown) with value "food".
When the "food" field is set to type "text" everything is fine. When I change the type of this
field to "dropdown" nothing appears.
To create custom page and custom fields I use Magic Fields plugin in Wordpress.
Here's the code:
<?php
global $wp_query;
query_posts(array(
'post_type' => 'products',
'meta_query' => array(
array(
'key' => 'product_category',
'value' => 'food',
)
))
);
while(have_posts()) : the_post(); ?>
<?php $key = get_post_meta($post->ID, 'title'); ?>
<li <?php post_class(); ?>><?php if($key) { echo $key[0]; } else { the_title(); }; ?></li>
<?php
endwhile;
wp_reset_query();
?>
use meta_key and meta_value
<?php
global $wp_query;
query_posts(array(
'post_type' => 'products',
'meta_query' => array(
array(
'meta_key' => 'product_category',
'meta_value' => 'food',
)
))
);
?>
You need to add the parameter metacompare to the array (meta_compare => 'like') because on table wp_postmeta the key_value is saved like "a:1:{i:0;s:7:"Food";}"
so
you should change to:
query_posts(array(
'post_type' => 'products',
'meta_key' => 'product_category',
'meta_value' => 'food',
'meta_compare' => 'like'
))
);
Try using meta_query instead. Make sure you try both products and product as your post_type parameter.
$args = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => 'product_category',
'value' => 'food'
)
)
);
$query = new WP_Query( $args );
Also, don't use query_posts. Here's why.

Resources