my controller
$data= $this->Contact->find('all',array(
'conditions' => array('Contact.User_id' => $id)));
$this->loadModel('Calllog');
foreach($data as $row){
$mobile= $row['Contact']['mobileNo'];
$home= $row['Contact']['homeNo'];
$work= $row['Contact']['workNo'];
$recent=$this->Calllog->find('all',
array('order'=>'Calllog.idCallLog DESC',
'limit' => 1,
'conditions' => array('Calllog.mobileNo' => array($mobile,$work,$home ),
'Calllog.User_id' => $id
)));
debug($recent);
$this->set('recent',$recent);
}
in my view i have this line
<?php foreach($recent as $as): ?> <?php echo $as['Calllog']['mobileNo'];?>
but isn't displaying anything even not any error
i debug the result in which i am getting one row means working fine
and i have also dump sql query in which query is running fine and when i run this query in phpmyadmin it is giving me the one row which is what i want .. but why it is not echoing the result on my view page
debug result
array(
(int) 0 => array(
'CallLog' => array(
'idCallLog =>'3',
'mobileNo'=>'12345'
'User_id'=>'1'
)))
and then after 4 lines
array()
Try this
$data= $this->Contact->find('all',array(
'conditions' => array('Contact.User_id' => $id)));
$recent = array();
$this->loadModel('Calllog');
foreach($data as $row){
$mobile= $row['Contact']['mobileNo'];
$home= $row['Contact']['homeNo'];
$work= $row['Contact']['workNo'];
$recent[]=$this->Calllog->find('all',
array('order'=>'Calllog.idCallLog DESC',
'limit' => 1,
'conditions' => array('Calllog.mobileNo' => array($mobile,$work,$home ),
'Calllog.User_id' => $id
)));
}
$this->set('recent',$recent);
debug($recent);
Related
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.
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 ?
I have written a custom related vacancies query for my site, using the plugin WP Job Manager. When using query monitor, I keep getting that the query is very slow (5+ seconds).
I've implemented query caching, except this requires the query to run at least 1 time.
Is it possible to speed up a query like this even more?
$location = get_post_meta($id, '_job_location', true);
$category = get_post_meta($id, 'job_category_wo', true);
$args = array(
'post_type' => 'job_listing',
'numberposts' => 5,
'meta_query' => array(
'relation' => 'OR',
array(
'key' => '_job_location',
'value' => $location,
'compare' => '='
),
array(
'relation' => 'AND',
array(
'key' => '_job_location',
'value' => $location,
'compare' => '='
),
array(
'key' => 'job_category_wo',
'value' => $category[0],
'compare' => 'LIKE'
)
)
)
);
$postslist = get_posts( $args );
In the example you show, you search for posts matching this condition:
_job_location = $region OR _job_location = $region AND job_category_wo LIKE $category[0]
You can simplify this expression to:
_job_location = $region
Boolean algebra says (A) OR (A AND B) is redundant. It's the same as simply (A).
This would factor out the OR operation, which is hard for SQL to optimize.
P.S.: I think there are other mistakes in your example. You set a variable $location but later you use $region. Which is it?
Also you are not sanitizing any values to prevent SQL injection.
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.
I am using WP_Query and json_encode to return the WordPress post data in a JSON array so that I can use Angular.js as a front end. Everything works, except that I am not able to include the featured image in the JSON file.
Here is my code:
<?php
function test_ajax()
{
header( "content-type: applications/json" );
$query = new WP_Query( array(
'numberposts' => 10,
'order'=> 'DESC',
'orderby' => 'title',
'post_type' => 'post',
'meta_query' => array(
array( 'key' => '_thumbnail_id')
) ) );
echo json_encode($query->get_posts());
die();
}
add_action("wp_ajax_nopriv_test_ajax", "test_ajax");
add_action("wp_ajax_test_ajax", "test_ajax");
Here is the json that is outputted:
[{
"ID":133,
"post_author":"1",
"post_date":"2014-08-19 05:54:03",
"post_date_gmt":"2014-08-19 05:54:03",
"post_content":"\"Woman",
"post_title":"Woman on the Banks of the Seine, After Courbet, 1950",
"post_excerpt":"",
"post_status":"publish",
"comment_status":"open",
"ping_status":"open",
"post_password":"",
"post_name":"woman-on-the-banks-of-the-seine-after-courbet-1950",
"to_ping":"",
"pinged":"",
"post_modified":"2014-08-19 05:54:03",
"post_modified_gmt":"2014-08-19 05:54:03",
"post_content_filtered":"",
"post_parent":0,
"guid":"http:\/\/localhost\/mabs\/?p=133",
"menu_order":0,
"post_type":"post",
"post_mime_type":"",
"comment_count":"0",
"filter":"raw"
}]