WordPress meta_query is not working - wordpress

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

Related

Filtered a query in WordPress

To display some items from a post type, I'm using this WordPress query:
$posts = get_posts(array(
'post_type' => 'realisations',
'status' => 'publish',
'order' => 'ASC'
));
But how can I filter the datas returned by this query depending the infos in the post type page ? For example, I have a input 'year' to get the year of the project.
Thanks.
You can use wp_query like Below
$args = array (
'post_type' => array( 'realisations' ),
'post_status' => array( 'publish' ),
'order' => 'ASC',
'orderby' => 'date',
'year' => 'yourinputyear' // 2021
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) { ?>
<?php while ( $query->have_posts() ) : $query->the_post();
echo get_the_title();
endwhile;
}else{
echo "Data not found";
}
Please try this way. Hope is useful.
Thanks
You can use Date Parameters check below code.
$posts = get_posts( array(
'post_type' => 'realisations',
'status' => 'publish',
'order' => 'ASC',
'date_query' => array(
array( 'year' => 'yourinputyear' )
)
) );

How do i show only 1 post from latest 10 post?

my english language is not good. sorry!
this code is for checkbox in advanced custom field plugin.
i want show only 1 post(randomly) from latest 10 post.
please help me. thanks
('posts_per_page' => 10) and ('numberposts' => 10) is not working.
<?php
$gallery = array(
"offset" => "0",
'showposts' => '1',
'orderby' => 'rand',
'meta_query' => array(
array(
'key' => 'postcat',
'value' => '"selection"',
'compare' => 'LIKE'
)));
// query
$qgallery = new WP_Query( $gallery );
?>
<?php if( $qgallery->have_posts() ): ?>
<?php while( $qgallery->have_posts() ) : $qgallery->the_post(); ?>
<div class="fromgallery">
<a href="0" class="frgall">
<span class="frgdesc"><?php the_title() ?></span>
</a></div>
<?php endwhile; ?><?php endif; ?>
Try this
$gallery = array(
'post_type' => 'post',
'posts_per_page' => 10,
'order' => 'DESC',
'no_found_rows' => 'true',
'_shuffle_and_pick' => 1
'meta_query' => array(
array(
'key' => 'postcat',
'value' => '"selection"',
'compare' => 'LIKE'
)));
$qgallery = new \WP_Query( $gallery );
I only do change in your code. For randomly one post you need to use '_shuffle_and_pick' => 1 , 'posts_per_page' => 10 is from 10 post and 'order' => 'DESC' is for latest posts. for custom '_shuffle_and_pick' you need to add
add_filter( 'the_posts', function( $posts, \WP_Query $qgallery )
{
if( $pick = $qgallery->get( '_shuffle_and_pick' ) )
{
shuffle( $posts );
$posts = array_slice( $posts, 0, (int) $pick );
}
return $posts;
}, 10, 2 );
To show Random post , Please use below script:
$args = array(
'post_type' => 'post',
'posts_per_page' => 10,
'orderby' => 'date',
'order' => 'DESC',
'no_found_rows' => 'true',
'_shuffle_and_pick' => 1
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'postcat',
'value' => '"selection"',
'compare' => 'LIKE'
)
)
);
$the_query = new WP_Query( $args );
//check is post found
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo get_the_title();
}
wp_reset_postdata();
} else {
echo 'no posts found';
}
I hope it will help you :)

How to query custom post tye from specific category

i need to display post from a custom post type but from one specific category, i use the code belowe but show me all post from all categories not just from 7.
<?php
$args = array( 'post_type' => 'tour', 'posts_per_page' => 10, 'cat=7' , 'taxonomy' => 'tourcat');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo "TEST TEST TEST TEST";
echo the_title();
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;
?>
This is not a valid argument for WP Query.
cat=7
Please read here about taxonomy queries, they should do what you require:
https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters
Currently your $args array simply holds an element that is cat=7, which is an incorrect way to pass arguments to the WP_Query constructor. Note that the issue is a little more obvious if you format your $args array with whitespace:
$args = array(
'post_type' => 'tour',
'posts_per_page' => 10,
'cat=7' , /* Here is the problem */
'taxonomy' => 'tourcat'
);
I believe your $args array should look like the following:
$args = array(
'post_type' => 'tour',
'posts_per_page' => 10,
'cat' => 7,
'taxonomy' => 'tourcat'
);
This,
$args = array( 'post_type' => 'tour', 'posts_per_page' => 10, 'cat=7' , 'taxonomy' => 'tourcat');
Should instead be like,
$args = array( 'post_type' => 'tour', 'posts_per_page' => 10, 'tax_query' => array( array( 'taxonomy' => 'tourcat','field' => 'ID','terms' => '7' ) ));
Try this :
$postData = new WP_Query(array(
'post_type' => 'tour', // custom post type
'posts_per_page'=>10,
'tax_query' => array(
array(
'taxonomy' => 'tourcat', //custom taxonomy name
'field' => 'id',
'terms' => 7
)
)
));
if($postData->have_posts()):
while ($postData->have_posts()): $postData->the_post();
echo "TEST TEST TEST TEST";
the_title();
echo '<div class="entry-content">';
the_content();
echo '</div>';
endwhile;
endif;

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?

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