Using "Advanced Custom Fields" checkbox fields in custom queries - checkbox

I’m trying to write a WP_Query which uses some data from two ACF checkboxes as part of the arguments.
I found the documentation showing how to use fields in custom queries however I cannot work out what the correct syntax for my checkboxes is.
My ACF’s:
Label: Promote to homepage?
Name: promote_to_homepage
Choices: Promoteto homepage : Promote to homepage
Label: Make feature?
Name: make_feature
Choices: Show as feature : Show as feature (top of homepage)
This is the query I have:
$the_query = new WP_Query(
array
(
'posts_per_page' => 1,
'meta_key' => 'promote_to_homepage',
'meta_value' => 'Promote to homepage',
'meta_key' => 'make_feature',
'meta_value' => 'Make feature'
)
);
I guess what I can’t figure out is why data is actually needed for meta_key and meta_value. Is the key the label? Is the value one of the choices? Or do I need to use meta_value => true or something? I have tried many variations and cannot get it to work.
Essentially what I want to do is output the most recent post that is checked for “Promote to homepage” and “Make feature”.
I have also tried:
array
(
'posts_per_page' => 1,
'meta_key' => 'promote_to_homepage',
'meta_value' => true,
'meta_key' => 'make_feature',
'meta_value' => true
)
Edit
This is the new code I have tried:
$args = array(
'post_type' => 'post',
'posts_per_page' => 1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'promote_to_homepage',
'value' => true,
),
array(
'key' => 'make_feature',
'value' => true,
),
)
);
$the_query = new WP_Query($args);
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<h2>' . get_the_title() . '</h2>';
echo '<p>' . get_the_excerpt() . '</p>';
echo '<p>Read more</p>';
}
}
wp_reset_postdata();

Another option is to get a lot of posts like this:
$args = array(
'post_type' => 'posts',
);
$posts = get_posts($args);
foreach($posts as $item) :
$make_feature = get_post_meta($item->ID, 'make_feature', true );
var_dump($make_feature); //test
$promote_to_homepage = get_post_meta($item->ID, 'promote_to_homepage', true );
var_dimp($promote_to_homepage); //test
if(isset($make_feature) && isset($promote_to_homepage)):
print_r($item);
endif;
endforeach;
Check if this works first before limiting posts. If you don't get any results from var_dump variables, there's something up with ACF.

$args = array(
'post_type' => 'post', //replace 'post' with cpt if you need to.
'posts_per_page' => 1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'promote_to_homepage',
'value' => '1',
'compare' => '=='
),
array(
'key' => 'make_feature',
'value' => '1',
'compare' => '=='
),
)
);
$the_query = new WP_Query($args); // This will return posts and other data
$the_query = get_posts( $args ); // This will return the posts
How's that for ya? Choose WP_Query OR get_posts: get_posts will return the posts data from wp_query anyway so you may as well just use that.
I'm not entirely sure what you mean by 'true/yes' but you can play around with the values. If the key value in the custom field is literally 'true/yes' then you better have that as the value in the args - otherwise yes or true will do.

Related

How can I get an array of custom categories in Wordpress?

I have registered a custom taxonomy as part of my custom post type, but when passing it through to get_categories() it returns an empty array. Any ideas as to why?
// Register FAQ Categories taxonomy
function bv_faq_register_categories() {
register_taxonomy(
'faq-category',
'faq',
array(
'label' => 'Categories',
'rewrite' => array('slug' => 'faq-category'),
'hierarchical' => true
)
);
}
add_action('init', 'bv_faq_register_categories');
// Category view
$categories = get_categories(array(
'taxonomy' => 'faq-category'
));
$categories is returning an empty array.
Have you tried get_terms instead?
$categories = get_terms( 'faq-category', array(
'orderby' => 'count',
'hide_empty' => 0
) );
Like #AD Styles said I would use get_terms using the custom taxonomy, to expand a bit here's some example code:
<?php
$post_type = 'faq-category';
// Get all the taxonomies for this post type
$taxonomies = get_object_taxonomies( (object) array( 'post_type' => $post_type ) );
foreach( $taxonomies as $taxonomy ) :
// Gets every "category" (term) in this taxonomy to get the respective posts
$terms = get_terms( array(
'taxonomy' => $taxonomy,
'parent' => 0
) );
foreach( $terms as $term ) :
echo "<h1>".$term->name."</h1>";
endforeach;
endforeach;
?>
Your code looks ok. Do you have assigned this category to any post/post type?
If not then you will get an empty result. for testing you can set 'hide_empty' = false like this:
// Category view
$categories = get_categories(array(
'taxonomy' => 'faq-category',
'hide_empty' => false // set it true
));
Also, you can use get_terms() function.

Show posts that have a with a certain meta_key value or not meta_key at all

I've got this loop where I need to display all the titles of posts that have a certain meta_value, or that and don't have the meta_key 'the_status'.
My posts currently use the meta_key called 'the_status' and can have either of these meta_key values:
helping
not_helping
finished_helping
...or a post may not even have the meta_key 'the_status' at all.
Here's what I have:
<?php
$the_query = array(
'posts_per_page' => -1,
'author' => $current_user->ID,
'post_status' => 'publish',
'meta_key' => 'the_status',
'meta_value' => array('helping')
);
$help_posts = new WP_Query($the_query);
while($help_posts->have_posts()) : $help_posts->the_post();
?>
<p><?php the_title(); ?></p>
<?php
endwhile;
?>
This obviously only gives me titles of posts that have a meta_value 'helping', but it also needs to show the titles of posts that don't have a meta_key 'the_status' at all.
Thanks for reading.
replace
'meta_key' => 'the_status',
'meta_value' => array('helping')
With
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'the_status',
'compare' => 'NOT EXISTS',
'value' => '' //can be omitted in WP 3.9+
),
array(
'key' => 'the_status',
'value' => array('helping')
)

Cakephp Pagination in DropDown?

I'm looking for a way, to handle the Pagination-options in a dropdown menu.
I'd like my users to select the sorting order in the same form with my filtering options, so when they hit "Send", pagination-order is already set.orm
e.g. like:
$this->Form->input('paginationorder',array(
'label' => 'order',
'options' => array(
'sort:id/direction:desc' => 'New Items, desc',
'sort:id/direction:asc' => 'New Items,asc',
),
'div' => false
)
);
$(function() {
$('#sort-properties').change(function() { // replace the ID_OF_YOUR_SELECT_BOX with the id to your select box given by Cake
var price = $(this).val();
window.location = baseUrl + 'properties/property_view/'+price;
});
});
<?php
echo $this->Form->input('orderby', array(
'id' => 'sort-properties',
'options' => array(
'sort:sale_rent_price/direction:asc' => 'Sort by Price Low to High',
'sort:sale_rent_price/direction:desc' => 'Sort by Price High to Low',
'sort:created/direction:asc' => 'Sort by Date Old to New',
'sort:created/direction:desc' => 'Sort by Date New to Old'
),
'label' => false,
'empty' => 'Default Order'
));
?>
I would change the values of the options to e.g. fieldname.desc, like :
$this->Form->input('paginationorder',array(
'label' => 'order',
'options' => array(
'id.desc' => 'New Items, desc',
'id.asc' => 'New Items,asc',
),
'div' => false
)
);
Afterwards in the controller put the values to an array via the explode method:
$order = explode(".", $this->request->data['Model']['field']);
then you can use the values in your find condition, like:
$result = $this->Model->find('all', array(
'order' => array( $order[0] => $order[1] )
));
There is propably a more elegant way to make this, but this should work.

Wordpress query_post array with Advanced Custom Fields (probably a simple solution)

Working on a Wordpress site and was wondering if anyone could point me in the right direction.
I have the following query_post which filters posts on my template and its working great.
query_posts( array( 'category_name' => 'galoretv', 'post_type' => 'post', 'paged'=>$paged, 'showposts'=>0, ) );
How would I append this to include a check for a specific value from Advanced Custom Fields?
Posts in this category have a radio button with three options 'primary' 'secondary' 'standard'
I want to be able to check against each value i.e if you belong in 'galoretv' and 'standard' do this.
I have the page executing and sorting with the parameters above, just not sure how to add the ACF value. I was able to get it to work using the sticky option but thats gonna work cause i need to have primary and secondar optionality. This is show i had it working with the sticky.
query_posts( array( 'category_name' => 'galoretv', 'post_type' => 'post', 'paged'=>$paged, 'showposts'=>0, 'post__in'=>get_option('sticky_posts')) );
The radio buttons field is called 'landing-grid-placement'
Any thoughts? Been checking the documentation, can't figure it out.
http://www.advancedcustomfields.com/docs/field-types/checkbox/
Thought this would work but it didn't
query_posts( array( 'category_name' => 'galoretv', 'post_type' => 'post', 'paged'=>$paged, 'showposts'=>0, 'landing-grid-placement' => 'primary') );
Any help would be greatly appreciated. This is probably a simple syntax issue but its eluding me and causing me a lot of issues. Been searching for an answer but haven't got it right yet. Thank you advance to whoever reads this and a double thank you to anyone who contributes a solution.
APPENDED CODE PER NOTE BELOW
<?php
$args = array(
'post_type' => 'post',
'category-slug' => 'models-galore',
'showposts'=>1,
'meta_query' => array(
array(
'key' => 'grid_location',
'value' => 'primary',
'compare' => '=',
'type' => 'CHAR'
)
)
);
$query = new WP_Query($args);
if($query->have_posts()) {
while($query->have_posts()) {
$query->the_post();
?>
<li class="span8">
<div class="thumbnail">
<?php echo get_the_post_thumbnail( get_the_ID(), 'media-large-thumb' ); ?>
<h3>
<?php the_title(); ?>
</h3>
</div>
</li>
<?php
}
}
?>
You can do this with a meta_query
Here's the doc
And here's an example:
$args = array(
//...
'post_type' => 'post',
'paged'=>$paged,
//...
'meta_query' => array(
array(
'key' => 'landing-grid-placement',
'value' => 'primary',
'compare' => '=', //default
'type' => 'CHAR' //default
)
)
);
//This is the shoposts option (deprecated since 2.1, now use posts_per_page)
$args['posts_per_page'] = -1; //-1 = infinite
//to add taxonomy
$args['tax_query'] = array(
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'galoretv'
)
);
$query = new WP_Query($args);
if($query->have_posts()) {
while($query->have_posts()) {
$query->the_post();
?>
<li class="span8">
<div class="thumbnail">
<?php echo get_the_post_thumbnail( get_the_ID(), 'media-large-thumb' ); ?>
<h2>
<?php the_title(); ?>
</h2>
</div>
</li>
<?php
}
}
Maybe there is a simpler solution for you :
$args = array(
//...
'post_type' => 'post',
'paged'=>$paged,
//...
'meta_key' => 'landing-grid-placement',
'meta_value' => 'primary',
'meta_compare' => '=' //default
)
);
$query = new WP_Query($args);
if($query->have_posts()) {
while($query->have_posts()) {
$query->the_post();
//do what you would normally do in your loop
}
}

Drupal allowed_values_function does not get called when creating a field

For some reason my allowed_values_function never gets called when showing a field on a user bundle. Code:
function get_business_units()
{
$options = entity_load('business_unit', FALSE, NULL, FALSE);
$opt = bu_to_list_values($options);
return $opt;
}
function MYMODULE_enable()
{
if (!field_info_field('field_user_business_unit')) {
$field = array(
'field_name' => 'field_user_business_unit',
'type' => 'text',
'settings' => array(
'allowed_values' => array(),
'allowed_values_function' => 'get_business_units',
)
);
field_create_field($field);
// Create the instance on the bundle.
$instance = array(
'field_name' => 'field_user_business_unit',
'entity_type' => 'user',
'label' => 'Business Unit',
'bundle' => 'user',
'required' => FALSE,
'settings' => array(
'user_register_form' => 1,
),
'widget' => array(
'type' => 'options_select',
),
);
field_create_instance($instance);
}
}
The field is created, and even displayed on the users "edit" page when editing their info. But the only value is "Select" or "None". My method is never called (I even placed a debug point). This is all in MYMODULE.install file.
The problem is: 'type' => 'text'.
You have to use: 'type' => 'list_text'.
Allowed values is meaningless for a text type.
Your get_business_units() function needs to be in the MYMODULE.module file; the .install files aren't included in a normal Drupal bootstrap.
Have you tried
drush features-revert MYMODULE ?

Resources