Order posts by Title length in WP_QUERY - wordpress

I am having problem ordering the posts based on the title length. Here is my code:
<?php
$terms = get_terms(array(
'taxonomy' => 'vendor_category',
'slug' => 'venues',
'hide_empty' => false
));
?>
<?php
foreach ($terms as $term) {
$eventargs = array(
'post_type' => 'vendor',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'meta_key' => 'primary_category',
'meta_value' => $term->term_id,
);
$eventqry = new WP_Query($eventargs);
?>
How can i sort the posts based on the title length in ascending order.

You can save title length in post meta on save_post hook. and then you can retrieve post order by post meta value.
You can use save_post hook to save the post meta. put this code in your active theme.
//for existing vendors
add_action('admin_init', 'udpate_existing_vendor');
function udpate_existing_vendor(){
$existing_vendor_updated = get_option('existing_vendor_updated', 'no');
if( $existing_vendor_updated == 'no' ){
$vendor_args = array(
'post_type' => 'vendor',
'post_status' => 'publish',
'posts_per_page' => -1
);
$vendors = new WP_Query( $vendor_args );
if( $vendors->have_posts() ) {
while ( $vendors->have_posts() ) { $vendors->the_post();
$length = strlen( get_the_title() );
update_post_meta( get_the_ID(), 'title_length', $length );
} wp_reset_postdata();
}
update_option('existing_vendor_updated', 'yes');
}
}
// for new vendor
function save_vendor_title_length( $post_id ) {
$length = strlen( get_the_title( $post_id ) );
update_post_meta( $post_id, 'title_length', $length );
}
add_action( 'save_post_vendor', 'save_vendor_title_length');
Here your query will look like.
$terms = get_terms(array(
'taxonomy' => 'vendor_category',
'slug' => 'venues',
'hide_empty' => false
));
foreach ($terms as $term) {
$eventargs = array(
'post_type' => 'vendor',
'posts_per_page' => -1,
'orderby' => 'meta_value_num',
'order' => 'ASC',
'meta_key' => 'title_length'
);
$eventqry = new WP_Query( $eventargs );
}

Related

Wordpress - Query comments by post meta

I am using the code below (simplified) to display a list of the last 10 comments:
<?php
$args = array(
'post_type' => 'tarefa',
'number' => '10',
'order' => 'DESC',
'orderby' => 'comment_date',
//'meta_key' => 'field_name',
//'meta_value' => 'field_value',
);
$comments_query = new WP_Comment_Query;
$comments = $comments_query->query( $args );
foreach ( $comments as $comment ) {
echo '<p>';
echo get_the_title($comment->comment_post_ID) . '<br>'; //post title
echo $comment->comment_content; // comment content
echo '</p>';
};
?>
Question:
Well, meta_key and meta_value seem to be associated to comment_meta... But in my case, I have to display comments based on post_meta key and value.
Any sugestions?
You can try this code.
You need to add a query for posts to get array of post ides with meta key.
Then use that array into comments query argument.
//QUERY FOR POSTS WITH META KEY AND VALUE (META QUERY)
$post_args = array(
'post_type' => 'post',
'meta_key' => 'meta key',//Meta key of post
'meta_value' => 'meta value',//String or Numeric value
'meta_compare' => '=',
);
$post_query = new WP_Query( $post_args );
$posts_array= array();
if ( $post_query->have_posts() ) {
while ( $post_query->have_posts() ) {
$post_query->the_post();
$posts_array[] = get_the_ID(); //Array of post ids
}
wp_reset_postdata();
}
//YOUR COMMENT ARGS SHOULD BE THIS
$args = array(
'post_type' => 'tarefa',
'number' => '10',
'order' => 'DESC',
'orderby' => 'comment_date',
'post__in' => $posts_array, //THIS IS THE ARRAY OF POST IDS WITH META QUERY
);
Try this , then let me know the result.
My first question here at Stackoverflow and that worked perfectly.
Thank you very much, Souvik!
Below the final result (simplified):
$post_args = array(
'post_type' => 'tarefa',
'posts_per_page' => -1,
'meta_key' => 'field_name',
'meta_value' => 'field_value',
);
$post_query = new WP_Query( $post_args );
$posts_array= array();
if ( $post_query->have_posts() ) {
while ( $post_query->have_posts() ) {
$post_query->the_post();
$posts_array[] = get_the_ID(); //Array of post ids
}
wp_reset_postdata();
}
//YOUR COMMENT ARGS SHOULD BE THIS
$args = array(
'number' => '30',
'order' => 'DESC',
'orderby' => 'comment_date',
'post__in' => $posts_array, //THIS IS THE ARRAY OF POST IDS WITH META QUERY
);
$comments_query = new WP_Comment_Query;
$comments = $comments_query->query( $args );
foreach ( $comments as $comment ) {
echo '<p>';
echo get_the_title($comment->comment_post_ID) . '<br>'; //post title
echo $comment->comment_content; // comment content
echo '</p>';
};

Woo-Commerce Product Query with Multiple Arguments

I am trying to Query Woo-Commerce products based on the following
Featured
Popular
Promotional
Free
I was working to show them in isotop so I have to get all products and give specifiq tag based on my query to make filter. I was confused if I can make 4 Query as I have 4 different arguments / parameter.
I made 4 different query and got result in 4 different array. But there have problems.
I had done 4 Queries and Save result on 4 array. So there same products containing. I have to filter all products and if same product in more than one category I have to input tag like features free . as Isotop works.
If I unique marge array it will not show products in multiple category. But same products can be in two categories or can be in all categories so it should add tag in duplicate product and clear duplicate products list so that in result there will be unique product with multi tag.
Any idea how to do that ?
Below is my code which providing all products.
<?php
foreach ($filter_attr as $item) {
if ($item == 'featured') {
$meta_query = WC()->query->get_meta_query();
$tax_query = WC()->query->get_tax_query();
$tax_query[] = array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
'operator' => 'IN',
);
$query_args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => '12',
'orderby' => 'date',
'order' => 'desc',
'meta_query' => $meta_query,
'tax_query' => $tax_query,
);
$loop = new WP_Query($query_args);
if ($loop->have_posts()) {
while ($loop->have_posts()) : $loop->the_post();
$featured_product = array(
"type"=>$item,
"product" => get_the_ID(),
);
endwhile;
} else {
echo __('No products found');
}
wp_reset_postdata();
} elseif($item == 'popular'){
$query_args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => '12',
'meta_key' => 'total_sales',
'orderby' => 'meta_value_num'
);
$loop = new WP_Query($query_args);
if ($loop->have_posts()) {
while ($loop->have_posts()) : $loop->the_post();
$id=array();
$popular_product = array(
"type"=>$item,
"product" => get_the_ID(),
);
print_r($popular_product);
endwhile;
} else {
echo __('No products found');
}
wp_reset_postdata();
} elseif($item == 'promotional'){
$query_args = array(
'posts_per_page' => 12,
'post_type' => 'product',
'meta_key' => '_sale_price',
'meta_value' => '0',
'meta_compare' => '>='
);
$loop = new WP_Query($query_args);
if ($loop->have_posts()) {
while ($loop->have_posts()) : $loop->the_post();
$id=array();
$promotional_product = array(
"type"=>$item,
"product" => get_the_ID(),
);
print_r($promotional_product);
endwhile;
} else {
echo __('No products found');
}
wp_reset_postdata();
} elseif($item == 'freebies'){
$query_args = array(
'posts_per_page' => 12,
'post_type' => array('product', 'product_variation'),
'meta_query' => array(
array(
'key' => '_price',
'value' => 0,
'compare' => '<=',
'type' => 'NUMERIC'
)
)
);
$loop = new WP_Query($query_args);
if ($loop->have_posts()) {
while ($loop->have_posts()) : $loop->the_post();
$id=array();
$freebies_product = array(
"type"=>$item,
"product" => get_the_ID(),
);
print_r($freebies_product);
endwhile;
} else {
echo __('No products found');
}
wp_reset_postdata();
}
}
?>

Exclude parent posts and display only child posts in archive

I have done this query and is working.I have a lot of child posts and i plan to display only child posts when listing the archive page of my custom post type city-guide.
$args = array(
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'city-guide',
'posts_per_page' => 36,
'paged' => $paged
);
$query = new WP_Query( $args );
?>
<?php $i=1; while( $query->have_posts() ): $query->the_post(); ?>
{
.....
}
I have tried
$all = get_posts(array('post_type'=> 'city-guide', 'posts_per_page' => -1));
$parents = array();
foreach ($all as $single)
{
$kids = get_children($single->ID);
if(isset($kids) && !empty($kids) && count($kids) >= 1)
{
$parents[] = $single->ID;
}
}
$args = array(
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'city-guide',
'post__not_in' => $parents,
'posts_per_page' => 36,
'paged' => $paged
);
$query = new WP_Query( $args );
?>
<?php $i=1; while( $query->have_posts() ): $query->the_post(); ?>
{
....
}
This did not work.Please help me find out where i went wrong.
I know it's an old question but hoping I can help someone that finds their way here looking for the same thing I was.
You can show ONLY child posts by excluding any posts with post_parent = 0 using the 'post_parent__not_in' argument:
$args = array(
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'city-guide',
'posts_per_page' => 36,
'paged' => $paged,
'post_parent__not_in' => array(0)
);
This avoids the need to loop thru each parent post to get each child.
I see you are trying to push the IDs into an array but why not just use the IDs while you are looping through them while getting the children within the loop at the same time? The example below is how I would tackle this.
<?php
$args = array(
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'city-guide',
'posts_per_page' => 36,
'paged' => $paged
);
$query = new WP_Query( $args );
$i=1; while( $query->have_posts() ): $query->the_post();
$parentID = get_the_ID();
$childrenArgs = array(
'post_type' => 'page',
'post_parent' => $parentID ,
);
$children = get_children($childrenArgs);
foreach ($children as $child){
echo '<h1>' . $child -> post_title . '</h1>';
$content = $child -> post_content;
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]>', $content);
echo $content;
}
endwhile;
?>
I think you need to look into the action pre_get_posts. Something like this in your functions.php would do the trick.
function namespace_custom_query_vars( $query ) {
if ( !is_admin() && $query->is_main_query()) {
if ( $query->query["post_type"] == 'custom_post_type' ) {
$query->set( 'post_parent__not_in', 0 );
}
}
return $query;
}
add_action( 'pre_get_posts', 'namespace_custom_query_vars' );
There's a decent post about this here. Though note that the code on this page does not compile for small syntax errors.
What about using relations? A simple disjunctive union should do the charm.
$args = array(
'post_type' => POST_TYPE,
'posts_per_page' => 36,
'orderby' => 'date',
'order' => 'DESC',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => POST_TAXONOMY,
'field' => 'slug',
'terms' => $tax_slug,
'include_children' => true
),
array(
'taxonomy' => POST_TAXONOMY,
'field' => 'slug',
'terms' => $tax_slug,
'include_children' => false,
'operator' => 'NOT IN'
)
)
);
Or is there a reason why not to consider this?

In Wordpress, wp_query with special attribute

When I use the WP_Query, I want to filter them by titles' initial letter, Like I only want the post when the initial is between 'F-J', what should I do with it.
$query_arguments = array(
'post_type' => $atts['_type'],
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => $atts['postsPerPage'],
'ignore_sticky_posts'=> 1,
'paged' => $paged
);
$trombinoscope_query = new WP_Query($query_arguments);
You can try the mysql solution described here.
Something as :
<?php
$child_pages = $wpdb->get_results("SELECT * FROM $wpdb->posts WHERE (post_title like 'F%' OR post_title like 'G%' OR post_title like 'I%' OR post_title like 'F%') AND post_status='publish'");
if ( $child_pages ) : foreach ( $child_pages as $pageChild ) : setup_postdata( $pageChild );
//Your code comes here.
endforeach; endif;
?>
I add a meta_key, it's fine for me now.
like this
function set_meta_for_employe_post() {
$post_title = get_the_title();
$post_id = get_the_ID();
if ('employe' == get_post_type()) {
if($post_title) {
add_post_meta($post_id, 'initial_letter', $post_title[0], true);
}
}
}
add_action( 'save_post', 'set_meta_for_employe_post');
and after:
$query_arguments = array(
'post_type' => $atts['_type'],
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page' => $atts['postsPerPage'],
'ignore_sticky_posts'=> 1,
'paged' => $paged,
'meta_key' => 'initial_letter',
'meta_value' => $letters,
);

Using Wp-query with serialized postmeta values

I've stored some serialized data in wp-postmeta table, e.g.:
$data = array(
'details' => $dettagli,
'vernice' => $vernice,
'reperibile' => $reperibile,
'valore2' => $valore2,
'valuta2' => $valuta2,
'subcat' => $subcat
);
add_post_meta($post_ID, 'meta', $data);
I would like to extract all the posts with the key "details" and value = "4". How can I do it?
I've tried doing this code:
$args = array(
'post_type' => 'custom-post-type',
'posts_per_page' => -1,
'meta_query' =>
array(
'key' => 'details',
'value' => '4',
'compare' => 'LIKE'
),
'meta_key' => 'meta',
);
$query = new WP_Query( $args );
if ($query->have_posts()) {
while ($query->have_posts()) {
$query->the_post();
the_ID();
}
}
What is wrong?
meta_query is an array of arrays.
You might try this:
$args = array(
//parameters here
'meta_query' => array(
array(
'key' => 'details',
'value' => '4',
'compare' => 'LIKE'
)
),
//remove meta_key => 'meta'
);
however i'm not 100% sure if this could help you, let's try it, please let me know if you get stuck.
UPDATE
Serialized array in post meta are not good deals. So i suggest you to change your save function to store custom fields individually.
I've found a way to unserialize your previous post meta values:
$args = array(
'post_type' => 'collectable',
'meta_key' => 'meta',
'posts_per_page' => -1
);
$query = new WP_Query( $args );
if( $query->have_posts() ) : while( $query->have_posts() ) : $query->the_post();
//Get data for each post
$data = get_post_meta( $post->id, 'meta', true );
//Add post meta value individually for each field
add_post_meta( $post->ID, 'details', $data['details'] );
add_post_meta( $post->ID, 'vernice', $data['vernice'] );
add_post_meta( $post->ID, 'reperibile', $data['reperibile'] );
add_post_meta( $post->ID, 'valore2', $data['valore2'] );
add_post_meta( $post->ID, 'valuta2', $data['valuta2'] );
add_post_meta( $post->ID, 'subcat', $data['subcat'] );
//Delete serialized data
delete_post_meta( $post->ID, 'meta', $data );
endwhile; endif;
Now you should be able to query as you want.
Hope it helps!
Have you tried this .Just a try
$the_query = new WP_Query(array( 'meta_key' => 'open', 'meta_value' => 'yes' ));

Resources