Query posts custom field: check list inside a ACF repeater - mysql

I'm trying to query all posts that have a specific check box selected inside a repeater field.
I found this tutorial and what I want to do is a mix of topic 3 and 4, but I couldn't accomplish by my self, my first attempt was to query all posts and print just one checked item:
<?php
$args = array(
'post_type' => 'modalidade',
'posts_per_page' => -1,
);
$loop = new WP_Query( $args );
?>
<?php
// check if the repeater field has rows of data
if( have_rows('horarios') ):
// loop through the rows of data
while ( have_rows('horarios') ) : the_row();
$dia_da_semana = get_sub_field_object( 'dia_da_semana' );
$horario = get_sub_field_object( 'horario' );
if ( in_array(1, $dia_da_semana['value'])) {
echo 'segunda';
print_r( $horario['value']);
echo '<br>';
}
endwhile;
endif;
?>
But don't think that is a good solution. Maybe there is a better way to achieve what I'm trying to do.

I used the tutorial found here and tweked my code and my data structure a little bit and made it. The final code looks like this:
// filter
function my_posts_where( $where ) {
$where = str_replace("meta_key = 'time_%", "meta_key LIKE 'time_%", $where);
return $where;
}
add_filter('posts_where', 'my_posts_where');
// args
$args = array(
'posts_per_page' => -1,
'post_type' => 'class',
'meta_query' => array(
array(
'key' => 'time_%_day_of_week',
'value' => 'monday',
'compare' => 'LIKE'
),
)
);
One change that was very important to me was to replace the 'numberposts' => -1, to 'posts_per_page' => -1, to get all the posts. The property numberposts wasn't working.

Related

show query meta_key_value including null value

I am trying to do a simple query in WordPress ordered by a meta_value_num of a custom field, the problem is that not all the inputs have a value so it gives me a null value and it does not show very well in the query, it only shows the ones that have value, I have this code
<ul class="post">
<?php $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'post',
'paged' => $paged,
'meta_key' => 'votes_count',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'post_status' => 'publish'
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
get_template_part( 'public/partials/template/loop' );
endwhile; ?>
<li class="numeration">
<div class="paginavi">
<?php YESPLEASE_Add_Theme_Support::yesplease_pagination(); ?>
</div>
</li>
<?php endif;?>
</ul>
You can use meta_query
Try this below code.
$args = array(
'post_type' => 'post',
'paged' => $paged,
'meta_key' => 'votes_count',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'votes_count',
'value' => array(''),
'compare' => 'NOT IN'
)
)
);
It only shows the ones that have the value because that is precisely what you're asking it to do.
All I can think of to counter that is to make it a required field in the admin panel, or finding some other way of including it in the post object using, for example, ACF.
ACF is the most powerful plugin there is on the topic of adding custom fields that are easy to access in all post types, with extensive and easy-to-read documentation. By using ACF you can make it a required value in the post edit screen, or you can make it default to an actual value, like 0 instead of NULL.

Populate woocommerce_wp_select field from database

please help me out with this..I've tried too many different things...
I'm trying to populate a woocommerce select field with data from the database.
// Add fields & settings to the custom product tab
$SQL = "SELECT DISTINCT table_name FROM wp_lbc_prices";
$array = $wpdb->get_results( $SQL, ARRAY_A);
add_action( 'woocommerce_product_data_panels',
'wcpt_roller_blind_options_product_tab_content' );
function wcpt_roller_blind_options_product_tab_content() {
?><div id='roller_blind_options' class='panel woocommerce_options_panel'><?php
?><div class='options_group'><?php
woocommerce_wp_select( array(
'id' => 'roller_blind_tables',
'label' => __( 'Price Tables', 'wcpt' ),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __( 'Select Associated Price Table.', 'wcpt' ),
'options' => $array
));
?></div>
</div><?php
Certainly, the query to the DB works and a result is being returned... but I'm not sure how to make an acceptable array (I'm used to asp.net which seems to make this more simple!). The data I am returning does not need an ID so the value and text of the dropdown can be the same.
You need first to be sure of table_name or replace it by the correct column slug to be queried from your database table wp_lbc_prices.
You need to replace the WPDB method get_results() by get_col()which query a single column and gives an array natively.
Prepare the array to copying the values to the keys using array_combine()
I have completed your code with the hooked function that adds a custom product tab.
Your revisited code will be:
add_filter( 'woocommerce_product_data_tabs', 'add_roller_blind_product_data_tab' );
function add_roller_blind_product_data_tab( $tabs ) {
$tabs['roller_blind'] = array(
'label' => __( 'Roller blind', 'wcpt' ),
'target' => 'roller_blind_options', // <== to be used in the <div> class of the content
//'class' => array('show_if_simple'), // or 'hide_if_simple' or 'show_if_variable'…
);
return $tabs;
}
add_action( 'woocommerce_product_data_panels', 'display_roller_blind_product_data_tab_content' );
function display_roller_blind_product_data_tab_content() {
global $wpdb;
echo '<div id="roller_blind_options" class="panel woocommerce_options_panel">
<div class="options_group">';
$sql_query = $wpdb->get_col( "SELECT DISTINCT table_name FROM {$wpdb->prefix}wp_lbc_prices" );
$options = sizeof($sql_query) > 0 ? array_combine( $sql_query, $sql_query ) : array();
woocommerce_wp_select( array(
'id' => 'roller_blind_tables',
'label' => __( 'Price Tables', 'wcpt' ),
'placeholder' => '',
'desc_tip' => 'true',
'description' => __( 'Select Associated Price Table.', 'wcpt' ),
'options' => $options
));
echo '</div></div>';
}
It should better work now. But as this is a custom database table is not testable.
get_results function return an array with indexes so the values can be like array of objects like this array([0]=>object,[1]=>object) can you please share what you are getting in $array variable ?

How to return a JSON with an array in object using PHP json_encode()

I'm trying to return JSON values like below using PHP:
{"a":"valuea","b":"valueb","c":[{"c1":"valuec1","c2":"valuec2"}]}
following are the PHP code
<?php
$data = array(
'a' => 'valuea',
'b' => 'valueb',
'c' => array(
'c1' => 'valuec1',
'c2' => 'valuec2',
),
);
echo json_encode($data);
Then I got this:
{"a":"valuea","b":"valueb","c":{"c1":"valuec1","c2":"valuec2"}}
Any ideas?
Might be ugly but here it is.
<?php
$data = array(
'a' => 'valuea',
'b' => 'valueb',
'c' => array(
array(
'c1' => 'valuec1',
'c2' => 'valuec2')
),
);
echo json_encode($data);
tested it using the http://phpfiddle.org/;
The idea is the first array you create for c will not be considered as a collection because that is an OBJECT. Making a nested array inside the first array for c will make the c as a collection.

Move Wordpress Custom Post Type Content to a Custom Field Value

Hey all I have a 163 posts imported into a custom post type from a csv file, it's all great except one little problem. The plugin forced me to put a column into the content field - so I chose one that I was going to put as a custom field - is there anyway for me to now convert all of them into a custom field and clear the content area?
Thanks!
You could put something like this (untested) in functions.php
add_action( 'get_header', 'so19967768_update_posts' );
function so19967768_update_posts()
{
if( ! isset( $_GET['update'] ) )
return;
$posts = get_posts( array( 'post_type' => 'my_post_type', 'posts_per_page' => -1 ) );
foreach( $posts as $post )
{
setup_postdata( $post );
$value = $post->post_content;
update_post_meta( $post->ID, 'my_meta_key', $value );
wp_update_post( array(
'ID' => $post->ID,
'post_content' => ''
) );
}
echo 'update complete!';
die();
}
and visit yoursite.com/?update=1 to run the update

Wordpress -how to return just current month posts on archive.php

How to modify the wp_query to take in count post by month as per URL 2012/10/ ?
The following will list ALL post, where I just want to list post for 2012/10/
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'posts_per_page' => 10, 'paged' => $paged, 'orderby' => 'date', 'order' => 'DESC' );
$wp_query = new WP_Query($args);
while ( have_posts() ) : the_post();
Any suggestions much appreciated.
There's a code snippet provided in the link from the earlier answer that says "Returns posts for just the current week". Change "week" for "month", and you got your answer.
The code snippet would look like this:
$year = date('Y');
$month = date('n');
$query = new WP_Query( 'year=' . $year . '&monthnum=' . $month );
Just read the link. It's all there.
EDIT: If there are further problems returning the month, it may be because the variable needs a leading zero. In that case, the $month variable should be
$month = date('m');
You can do that with the year and monthnum parameters, as described in the section Time Parameters in the class reference:
// ... set up your argument array for WP_Query:
$args = array(
'posts_per_page' => 10,
'paged' => $paged,
'orderby' => 'date',
'order' => 'DESC' ,
// add the year/month query here:
'year' => 2012,
'monthnum' => 10
);
$wp_query = new WP_Query($args);
// ... and so on