Using Wp-query with serialized postmeta values - wordpress

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' ));

Related

Order posts by Title length in WP_QUERY

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 );
}

Howto remove all posts that do not have a Featured images assigned to them?

This finds all of those:
$args = array(
'meta_query' => array(
array(
'key' => '_thumbnail_id',
'value' => '?',
'compare' => 'NOT EXISTS'
)
),
);
$new_query = new WP_Query( $args );
How to make a mini plugin for when I activate it, it deletes all posts without a featured images assigned to them?
I trying:
add_action( 'init', 'process_posts' );
function process_posts() {
$args = array(
'meta_query' => array(
array(
'key' => '_thumbnail_id',
'value' => '?',
'compare' => 'NOT EXISTS'
)
),
);
$new_query = new WP_Query( $args );
if (empty($_thumbnail_id)) {
wp_delete_post($_POST['post_id'], true);
}
}
Can someone show this to me, please?. Thanks
You can use Wordpress function wp_delete_post() to delete the posts. Create a for each loop getting the post id's and passing them to wp_delete_post(). I added this code to my functions.php file and it worked as expected. Because you have a lot of posts it may take some time to execute. It it takes too long you may have to adjust setTimeout in your php.ini file. Hope that helps!
$args = array(
'meta_query' => array(
array(
'key' => '_thumbnail_id',
'value' => '?',
'compare' => 'NOT EXISTS'
)
),
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
$post_id = get_the_ID();
wp_delete_post($post_id);
}
wp_reset_postdata();
}
Here is some example code of the hook you can use, you'll need to write your own loop code for deleting the posts.
add_action( 'init', 'process_posts' );
function process_posts() {
$args = array(
'meta_query' => array(
array(
'key' => '_thumbnail_id',
'value' => '?',
'compare' => 'NOT EXISTS'
)
),
);
$new_query = new WP_Query( $args );
// Delete your posts here with a loop
}

WordPress meta_query is not working

I have a problem on using meta_query in WordPress. the meta_key that iv'e tried is wpcargo_status and the value is Delivered. The problem is it is still getting the other status. This is what iv'e tried...
$wpc_report_args = array(
'post_type' => 'shipment',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'status',
'value' => 'Delivered',
'type' => 'CHAR',
'compare' => '=',
)
),
);
$the_query = new WP_Query( $wpc_report_args );
// The Loop
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
echo get_the_ID().'<br />';
echo get_post_meta(get_the_ID(), 'shipper_name', true).'<br />';
echo get_post_meta(get_the_ID(), 'status', true).'<br />';
endwhile;
endif;
// Reset Post Data
wp_reset_postdata();
On my database
Output of my query
Do you have any idea what is wrong with my code?
Updated
I already tried this and it is working but I need multiple meta_query
'meta_key' => 'status',
'meta_value' => 'Delivered',
'meta_compare' => '=',
Try this
$wpc_report_args = array(
'post_type' => 'shipment',
'meta_query' => array(
array(
'key' => 'status',
'value' => 'Delivered',
'compare' => '='
)
)
);
$the_query = new WP_Query( $wpc_report_args );
Type - Default value is 'CHAR'
Iv'e seen that there is no errors on your WP_Query. And there is a possible conflict on your query or there is something overriding on it.
Deactivate other plugins
Theme conflict
Check the parse_query - This hook will execute after WP_Query.
$wpc_report_args = array(
'post_type' => 'shipment',
'post_status' => 'publish',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'wpcargo_status',
'value' => 'Delivered',
'type' => 'CHAR',
'compare' => '=',
)
),
);
$the_query = new WP_Query( $wpc_report_args );
// The Loop
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
echo get_the_ID().'<br />';
echo get_post_meta(get_the_ID(), 'shipper_name', true).'<br />';
echo get_post_meta(get_the_ID(), 'status', true).'<br />';
endwhile;
endif;
// Reset Post Data
wp_reset_postdata();

custom post type loop not working

I am trying to create a custom loop from the metadata I have entered into the posts.
<?php $args = array (
'post_type' => array( 'movies' ),
'order' => 'DESC',
'order_by' => 'get_post_meta( get_the_ID(), "released", true )',
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) :
bd_pagination();
while ( $query->have_posts() ) : $query->the_post(); ?>
I so so need help plz
You need to tweak your parameters a bit. I'm assuming the "released" is a date field? If not (eg, if it's a timestamp), use a numeric orderby instead. But this should set you on your way:
<?php
$args = array(
'post_type' => array( 'movies' ),
'orderby' => 'meta_value_date',
'meta_key' => 'released',
'meta_type' => 'DATE'
);
$query = new WP_Query( $args );

wordpress advanced custom fields order posts by date-picker

For those familiar with the ACF plugin...
I have some events posts that are currently displaying in post order (see code below). I would like them to display in the order specified by the date-picker.
Can anyone tell me what to amend in the following - I have tried the documentation on the site, but my PHP is basic.
It says I need to add
'orderby' => 'meta_value_num',
But no joy.
<?php function le_whatson_aside() {
//THis loop is for the CPT
$args = array(
'post_type' => 'events', // enter your custom post type
'orderby' => 'menu_order',
'order' => 'ASC',
'posts_per_page'=> '10', // overrides posts per page in theme settings
'tax_query' => array(
array(
'taxonomy' => 'audience', //name of custom taxonomy
'field' => 'slug',
'terms' => 'everyone' //name of category
)
)
);
$loop = new WP_Query( $args );
if( $loop->have_posts() ):
?>
<div>
<h2>What's On</h2>
</div>
<div class="whatson entry-content">
<?php
while( $loop->have_posts() ): $loop->the_post(); global $post;
?>
<p class="whatson-date"><?php echo date("dS F Y",strtotime(get_field('date')));?></p>
<h4 class="whatson-title"><?php echo get_the_title(); ?></h4>
<?php
endwhile;
?>
</div>
<?php
endif; }
Thanks all.
Try
orderby=date or `post_date`
If not the easiest way is to save your custom field 'startdate' as a unix timestamp. To do this, add the following to your theme's functions.php
// CREATE UNIX TIME STAMP FROM DATE PICKER
function custom_unixtimesamp ( $post_id ) {
if ( get_post_type( $post_id ) == 'events' ) {
$startdate = get_post_meta($post_id, 'startdate', true);
if($startdate) {
$dateparts = explode('/', $startdate);
$newdate1 = strtotime(date('d.m.Y H:i:s', strtotime($dateparts[1].'/'.$dateparts[0].'/'.$dateparts[2])));
update_post_meta($post_id, 'unixstartdate', $newdate1 );
}
}
}
add_action( 'save_post', 'custom_unixtimesamp', 100, 2);
The do:
$today = time();
$args = array(
'post_type' => 'events',
'posts_per_page' => 5,
'meta_query' => array(
array(
'key' => 'unixstartdate',
'compare' => '>=',
'value' => $today,
)
),
'meta_key' => 'startdate',
'orderby' => 'meta_value',
'order' => 'ASC',
);
$query = new WP_Query( $args );
$events = $query->posts;
Got it from here
I spent hours looking for this and I can confirm it works. See my code below.
If you're trying for a loop on a page with other loops, with a bunch of template parts in there, and you would also like to sort by a category, it goes :
$today = time();
$the_query = new WP_Query( array(
'post_type' => 'events',
'posts_per_page' => 3,
'meta_query' => array(
array(
'key' => 'start',
'value' => $today,
'compare' => '>=',
)
),
'tax_query' => array(
array (
'taxonomy' => 'the_taxonomy',
'field' => 'slug',
'terms' => 'the-name'
)
),
'meta_key' => 'start',
'orderby' => 'meta_value',
'order' => 'ASC',
) );
while ( $the_query->have_posts() ) :
$the_query->the_post();
get_template_part( 'content-events' );
endwhile;
wp_reset_postdata();
Of course, you have to include Unix the function beforehand.
function custom_unixtimesamp ( $post_id ) {
if ( get_post_type( $post_id ) == 'events' ) {
$startdate = get_post_meta($post_id, 'start', true);
if($startdate) {
$dateparts = explode('_', $startdate);
$newdate1 = strtotime(date('d.m.Y H:i:s', strtotime($dateparts[1].'/'.$dateparts[0].'/'.$dateparts[2])));
update_post_meta($post_id, 'unixstartdate', $newdate1 );
}
}
}
add_action( 'save_post', 'custom_unixtimesamp', 100, 2);
I would approach it like this. Create a "named" meta query and then order by that query. The meta query uses "EXITS" to filter out posts that don't have a date set. This works with the ACF date picker when the dates are saved to the database using the default format d/m/Y. This approach also works with the datetime picker.
$query = new WP_Query([
"meta_query" => [
"custom_date" => [
"key" => "date",
"compare" => "EXISTS",
"type" => "DATETIME",
]
],
"orderby" => [
"custom_date" => "ASC",
],
]);
Be sure to update the value for key to whatever your ACF field name is.

Resources