I'm trying to sort post by ID in ASC order, I'm able to do that when I use single query but when use array query I'm unbale to sort post by ID in ASC order my code looks like below
<?php
$ourCurrentPage = get_query_var('paged');
$newposts = new WP_Query(array(
'category_name' => 'stock-market-basics',
'orderby' => 'ID',
'order' => 'ASC',
'paged' => $ourCurrentPage
));
?>
<?php
if ($newposts->have_posts()):
while ($newposts->have_posts()):
$newposts->the_post();
?>
// Some display code and after closing code //
<?php endwhile; else : ?>
<p><?php esc_html_e( 'Sorry, no results matched your criteria.' ); ?</p>
<?php endif; ?>
<li><?php
echo paginate_links(array(
'total' => $newposts->max_num_pages
));
?></li>
WordPress query by category name
<?php $args = array(
'posts_per_page' => 5,
'offset' => 0,
'category' => '',
'category_name' => '',
'orderby' => 'date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'author' => '',
'author_name' => '',
'post_status' => 'publish',
'suppress_filters' => true
);
$posts_array = get_posts( $args ); ?>
// Custom query.
$query = new WP_Query( $args );
// Check that we have query results.
if ( $query->have_posts() ) {
// Start looping over the query results.
while ( $query->have_posts() ) {
$query->the_post();
// Contents of the queried post results go here.
}
}
Hope this works for you.
Related
$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;
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.
I am trying to get the last inserted in id MySQL but it doesn't return anything. Row is getting inserted in the database and also $wpdb->last_query gives the last inserted query also.
Here is my code
global $wpdb;
$table_name = $wpdb->prefix . "request_from";
$wpdb->insert($table_name,
array('pre' => $prefix,
'first_name' => $first_name,
'middle_name' => $middle_initial,
'last_name' => $last_name,
'badge_name' => $name_badge,
'title' => $title,
'company' => $company,
'direct_mail' => $direct_mail,
'twitter' => $twitter_handle,
'direct_phone' => $direct_phone,
'address' => $address,
'address2' => $address_2,
'city' => $city,
'state' => $state,
'province' => $province,
'zip' => $zip_code,
'country' => $country,
'cc' => $cc,
'cc_contact' => $second_contact,
'cc_mail' => $second_email,
'cc_phone' => $second_phone)
);
$x= $wpdb->last_query;
$id = $wpdb->insert_id
I have a column called id with auto increment value
id int(11) No None AUTO_INCREMENT
Follow wpdb reference in Codex for troubleshooting:
enable database error display via $wpdb->show_errors()
check what query is being formed and run via $wpdb->last_query
Please check code by print or dump variable,
global $wpdb;
$table_name = $wpdb->prefix . "request_from";
$wpdb->insert($table_name,
array('pre' => $prefix,
'first_name' => $first_name,
'middle_name' => $middle_initial,
'last_name' => $last_name,
'badge_name' => $name_badge,
'title' => $title,
'company' => $company,
'direct_mail' => $direct_mail,
'twitter' => $twitter_handle,
'direct_phone' => $direct_phone,
'address' => $address,
'address2' => $address_2,
'city' => $city,
'state' => $state,
'province' => $province,
'zip' => $zip_code,
'country' => $country,
'cc' => $cc,
'cc_contact' => $second_contact,
'cc_mail' => $second_email,
'cc_phone' => $second_phone)
);
$x= $wpdb->last_query;
$id = $wpdb->insert_id;
// Let's output the last autogenerated ID.
echo $wpdb->insert_id;
// This returns the same result
echo mysql_insert_id();
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
}}
i am using this binding
$this->Company->bindModel( array(
'hasMany' => array(
'CompanyContactPerson'=>array('conditions' => array('Company.name Like'=>'%'.$a.'%'),),
),
'belongsTo' => array(
'Status',
'User'
)
));
$this->paginate = array(
'limit' =>20,
'conditions'=>$conditions,
'order' =>array('Company.id'=> 'desc') ,
);
$all_companies = $this->paginate('Company');
It give this query as a result.
SELECT `Company`.`id`, `Company`.`user_id`, `Company`.`name`, `Company`.`status_id`, `Company`.`email`, `Company`.`modified`, `Company`.`created`, `Status`.`id`, `Status`.`name`, `User`.`id`, `User`.`roll`, `User`.`username`, `User`.`email`, `User`.`password`, `User`.`first_name`, `User`.`last_name`, `User`.`gender`, `User`.`address`, `User`.`city`, `User`.`state`, `User`.`country`, `User`.`modified`, `User`.`created`
FROM `companyinfo`.`companies` AS `Company`
LEFT JOIN `companyinfo`.`statuses` AS `Status` ON (`Company`.`status_id` = `Status`.`id`)
LEFT JOIN `companyinfo`.`users` AS `User` ON (`Company`.`user_id` = `User`.`id`)
WHERE 1 = 1 ORDER BY `Company`.`id` desc LIMIT 20
it make seperate query for CompanyContactPerson model. so as a result i got all result from Company table. I need only those rows from company table where CompanyContactPerson conditions is satisfied . how i achieved this
put this in your Company model so it can have a relationship. :)
public $hasMany = array(
'CompanyContactPerson' => array(
'className' => 'CompanyContactPerson',
'foreignKey' => 'ForeignKeyinCompanyContactPerson',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => '',
'dependent' => true
)
));
or put this to your CompanyContactPerson Model
public $belongsTo = array(
'Company' => array(
'className' => 'Company',
'foreignKey' => 'ForeignKeyofCompanyContactPersonTABLE',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
Use limit in Query
'limit' => 2, //int
So only two rows will be returned.
Your code should be like
'CompanyContactPerson'=>array('conditions' =>
array('Company.name Like'=>'%'.$a.'%'),
'limit' => 2
),