Display the posts only if both the categories exist - mysql

Display the posts only if both the categories exist.
https://codex.wordpress.org/Class_Reference/WP_Meta_Query
How we can get posts list both category exits only?
bartags = 26,27
if (isset($_POST['bartags']) && !empty($_POST['bartags'])):
$tax_query[] = array(
array(
'taxonomy' => 'bar_tag',
'terms' => explode(',',$_POST['bartags']),
'compare'=>'IN', // what's use here?
)
);
endif;

if (isset($_POST['bartags']) && !empty($_POST['bartags'])):
$tax_query[] = array(
array(
'taxonomy' => 'bar_tag',
'terms' => explode(',',$_POST['bartags']),
'operator' => 'AND',
)
);
endif;
This one fixed using 'operator' => 'AND' which one not define on documents.

Related

How to get WooCommerce products from database based on multiple attributes

I want to get related products based on some criteria on product attributes like, the same sku, ean and gtin codes. But my query doesn't return any results. I tried different ways to query the database but all fail. Here is my query.
$sku = $product->get_sku();
$ean = $product->get_attribute( 'EAN' );
$gtin = $product->get_attribute( 'GTIN' );
$query = new WP_Query( array(
'post_type' => array('product'),
'posts_per_page' => 20,
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => '_sku',
'value' => $sku,
'compare' => '='
)
),
'tax_query' => array(
array(
'taxonomy' => 'pa_ean',
'field' => 'value',
'terms' => $ean,
'operator' => '=',
),
array(
'taxonomy' => 'pa_gtin',
'field' => 'value',
'terms' => $gtin,
'operator' => '=',
)
)
) );
By the way, if a product has ean code than it doesn't have a gtin and vice versa. It always has a sku which is unique to it self. But I have it added as a check to check if query at least returns value.
I can't find good documentary on this topic and hope someone here can help.

How to get count only today's post in get_terms wordpress

$termscat = get_terms([
'taxonomy' => 'newscategories',
'orderby' => 'count',
'order' => 'DESC',
'hide_empty' => false
]);
I am using above code to get post counts and is returning all post but i want to filter data only by todays date.
I think you want something like this.
SELECT count(post.ID) as count,rel.term_taxonomy_id,terms.name FROM {$wpdb->base_prefix}posts as post INNER JOIN {$wpdb->base_prefix}term_relationships as rel ON post.ID=rel.object_id INNER JOIN {$wpdb->base_prefix}terms as terms ON rel.term_taxonomy_id= terms.term_id WHERE post_date LIKE '$currentDate%' AND post.post_type='$post_type' GROUP BY rel.term_taxonomy_id ORDER BY count DESC
get_terms() used to get terms not posts.
if you want to get posts of current date then you can use the code below
$today = date( 'Y-m-d' );
$args = array(
'post_type' => 'vehicle',
'date_query' => array(
//set date ranges with strings!
'after' => 'today',
//allow exact matches to be returned
'inclusive' => true,
),
);
$query = new WP_Query( $args );
You can get more information on this https://codex.wordpress.org/Class_Reference/WP_Query#Date_Parameters
$today['year'] = current_time('Y');
$today['mon'] = current_time('m');
$today['mday'] = current_time('d');
$args = array(
'post_type' => 'news',
'date_query' => array(
array(
'year' => $today['year'],
'month' => $today['mon'],
'day' => $today['mday'],
),
),
'tax_query' => array(
array(
'taxonomy' => $tex_name,
'field' => 'term_id',
'terms' => $term_id,
),
),
);
$query = new WP_Query( $args );
$total = $query->found_posts;

cakephp virtual fields with subquery and if

I have a Category table with a name field.
I want to be able to overwrite that name using a PartnerCategory Table and join it.
So usually I would get the Category like this:
$options = array(
'contain' => array('PartnerCategory'),
'conditions' => array(
'Category.slug' => $slug,
'Category.status' => true,
)
);
$category = $this->Category->find('first', $options);
And then check, if ParentCategory.name is not empty and replace the Category.name with it.
Overwriting the name with php is tedious so I checked virtual fields.
I can overwrite the Category.name using this:
$this->virtualFields = array(
'name' => 'SELECT name FROM app_partner_categories WHERE category_id = 1 AND partner_id = 60'
);
But if the name in the PartnerCategory table is empty or no mathes are found, the Category.name would be null.
How can I overwrite Category.name only if PartnerCategory.name is found?
Not sure but this could be a possible solution:
$joins = array(array('table' => 'app_partner_categories',
'alias' => 'PartnerCategory',
'type' => 'INNER',
'conditions' => array(
'PartnerCategory.category_id = Category.id'
)
)
);
$options = array(
'fields' => array('IF(PartnerCategory.name IS NULL or PartnerCategory.name = "", Category.name, PartnerCategory.name) as overwritten_name'),
'joins' => $joins,
'conditions' => array(
'Category.slug' => $slug,
'Category.status' => true,
)
);
$category = $this->Category->find('first', $options);

ACF Field query by Repeater Field not empty

Been reading up on info for querying ACF Fields in wordpress here, having trouble with this bit though:
$args = array(
'posts_per_page' => -1,
'post_type' => 'team_member',
'status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'patents',
'value' => array(''),
'compare' => 'NOT IN'
)
)
);
$the_query = new WP_Query($args);
Basically, just trying to query all posts that have an ACF Repeater field, called patents that has at least 1 patent inside of it. How to do this?
I just had to deal with this myself, and what ended up working for me was querying for all posts where the value of the repeater field was greater than 0.
$args = array(
'posts_per_page' => -1,
'post_type' => 'team_member',
'status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'patents',
'value' => 0
'compare' => '>'
)
)
);
Mark's answer to search for 'patents' = 1 only returned posts where the repeater field had one value.
http://www.advancedcustomfields.com/resources/the_repeater_field/
Try to access the repeater field first, then call the WP_Query. If you were to do something like:
if( get_field('repeater_field_name', $post_id) )
Then call the sub_field and/or in this case the WP_Query.
There is also this syntax, you can gather all the posts you need then scroll through posts:
if( have_rows('repeater_field') ):
while( have_rows('repeater_field') ) : the_row();
$sub = get_sub_field('sub_field');
Maybe you can call the wp_query on this level?
just check if the value is true
$args = array(
'posts_per_page' => -1,
'post_type' => 'team_member',
'status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'patents',
'value' => true,
'compare' => '='
)
)
);
$the_query = new WP_Query($args);
Try this ..
This is not the standard solution for achieving the target but it will
work
Get all the post IDs whose repeater field(s) are there
<?php
$args = array(
'posts_per_page' => -1,
'post_type' => 'team_member',
'status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
);
$the_query = new WP_Query($args);
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
if ( have_rows('patents') ){
$allowed_ids[] = get_the_ID();
}
}
}
/* Restore original Post Data */
wp_reset_postdata();?>
Then run another Loop to get only those posts by their IDS
$args['post__in'] = $allowed_ids;
$the_query = new WP_Query($args);
// The Loop
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$patents = get_field('patents');//array
}}

Fetch random row from group by query (mysql) in Cakephp

I have a table 'activities' and the columns are grouped by three of its attributes. Now I want to fetch Activity ID that is randomly selected from all the set of groups. The query is as follows:
$act = $this->find('all', array('conditions' => $cond,
'limit' => $limit,
'fields' => array('count(*) as count, Activity.id as id'),
'page' => $page,
'group' => $group,
));
These are the things I tried and none of them worked.
1. Create a self join which should have random records
$this->bindModel(array(
'belongsTo' => array(
'Random' => array(
'className' => 'Activity',
'order' => 'rand()',
'foreignKey' => 'id',
'fields' => 'Random.id'
)
)
));
This failed because the rand() order is appended in the end which simply randomizes all fetched activities.
Added a join option
'joins' => array(
array(
'table' => 'activities',
'alias' => 'Random',
'type' => 'LEFT',
'conditions' => array(
'Activity.id = Random.id'
),
'fields' => 'Random.id',
'order' => 'rand()'
)
This also didn't work as order value is not parsed by Cake
Tried to add condition
'Activity.id >= floor(rand() * (max(Activity.id) - min(Activity.id)) - min(Activity.id))'
Gave a mysql error
Please help me out
Assuming that your query is working:
$act = $this->find('all', array('conditions' => $cond,
'limit' => $limit,
'fields' => array('count(*) as count, Activity.id as id'),
'page' => $page,
'group' => $group,
'order' => 'rand()',
'limit' => '1' // if you only want one random activity ID
));
Ok finally figured it out. We need to hack the way cake php works. In place of table name, I wrote a query to fetch randomly ordered records. Also notice the join type is 'right'. Left join wont produce randomly sorted list
$activities = $this->find('all', array('conditions' => $cond,
'page' => $page,
'limit' => $limit,
'fields' => array('count(*) as count, Random.id as id, max(Activity.modified) as Modified'),
'group' => $group,
'order' => 'Modified desc',
'joins' => array(
array(
'table' => '(select * from activities order by rand())',
'alias' => 'Random',
'type' => 'RIGHT',
'conditions' => array(
'Activity.id = Random.id'
),
)
)
));