Query only custom fields with wordpress - wordpress

I am executing the following WP_Query on my mysql database:
$products = new WP_Query(array(
'post_type' => 'posts',
'meta_query' => array(
array(
'key' => 'meta_key',
'compare' => '=',
'value' => '_cegg_data_Amazon',
),
),
));
When I var_dump($product) I am receiving a long wp_Query object. However, I would only like to receive an object/list of the custom post fields. The below SQL query gives me exactly that back within phpmyadmin:
SELECT * FROM `wp_postmeta` where meta_key="_cegg_data_Amazon" ORDER BY `wp_postmeta`.`meta_key` ASC
Any suggestions how to get only the values of the custom fields _cegg_data_Amazon. I appreciate your replies!

I think the easiest way would be to loop through your WP_Query object and create your array (or new obj) with the desired field:
$products = new WP_Query(array(
'post_type' => 'posts',
'meta_query' => array(
array(
'key' => 'meta_key',
'compare' => '=',
'value' => '_cegg_data_Amazon',
),
),
));
$cegg_data = array(); // initiate the new array
if( $products->have_posts() ) :
while( $products->have_posts() ) : $products->the_post();
$cegg_data[] = get_post_meta(get_the_ID(), '_cegg_data_Amazon', true); // populate array with the new value
endwhile;
endif;
wp_reset_query();
Now, if you var_dump "$cegg_data" it should contain an indexed array with the custom field values.
NB: the code is untested, just a quick draft ;)
Another and solution you can explore would be "posts_fields" filter documented here: https://developer.wordpress.org/reference/hooks/posts_fields/
Hope this helps and good luck ;)
Francesco

Related

WordPress custom query by meta_key

I'm trying to figure out how to achieve a specific query to modify my search results for posts with WordPress. I'm trying to search via a custom field called "Common Authors".
There can be multiple Common authors, which is causing my query to sometimes fail. Here is what I've got for now:
<?php
...
$query->set('meta_key', 'common_authors');
$query->set('meta_value', serialize( array(strval($_GET['common_author'])))); // I get a single ID from the url as a string
$query->set('meta_compare', 'IN');
This is what I see when I var_dump the query:
'meta_key' => string 'common_authors'
'meta_value' => string 'a:1:{i:0;s:5:"17145";}'
This works fine if there is only one common_author.
However, there can be multiple common_authors for a post. Here is a meta_value example from the database:
a:4:{i:0;s:5:"14409";i:1;s:5:"17145";i:2;s:5:"14407";i:3;s:5:"14406";}
Could somebody help me out, to figure out how to adapt my query, so that it would return this one as well?
Try This One Work Prefect!
$args = array(
'post_type' => 'post',
'meta_query' => array(
array(
'key' => 'common_authors',
'value' => array ( 'author1', 'author2', 'author3' ),
'compare' => 'IN'
)
)
);
$query = new WP_QUERY($args);
if You Need To Use It In pre_get_posts
$meta_query = array(
array(
'key' => 'common_authors',
'value' => array ( 'author1', 'author2', 'author3' ),
'compare' => 'IN'
)
);
$query->set('meta_query', $meta_query);

Issue on simple WP_Query with posts meta data

I have a custom post type 'event' and a meta field 'event_repeat'. I want to show only those events that have the 'weekly' value in 'event_repeat'.
My code for the query:
<?php
$args = array(
'post_type' => 'event',
'meta_query' => array (
'key' => 'event_repeat',
'value' => 'weekly',
'compare' => 'IN'
)
);
$events_future_query = new WP_Query($args);
?>
But the loop shows ALL events regardless the settings in 'meta_query'. I will be very thankful for you help.

How to query on custom fields in Wordpress?

I'm trying to query posts based on a custom field, and then display them in a loop. I've checked and double checked my code against the codex and other sources, but the query still does not appear to be working. What am I doing wrong?
Stripped down to the essentials, my code looks like this:
<?php
$args = array(
'meta_key' => 'my_custom_field'
);
$my_query = new WP_Query( $args );
?>
<?php if ( $my_query->have_posts() ) { ?>
<p>Success, we have posts!!!</p>
<?php } else { ?>
<p>Uh Oh, No posts!!!</p>
<?php } ?>
The conditional statement is dropping through and returning "Uh Oh, no posts".
I've checked the postmeta table, and there are definitely posts that contain the meta_key _my_custom_field. I have tried the query both with and without leading underscore.
What am I doing wrong?
I use this for search a date between two custom dates field in my custom post type "porfolio", i think that you are in a similar situation:
$args = array(
'post_type' => 'portfolio',
'posts_per_page' => '10',
'meta_query' => array(
array('key' => 'portfolio_start_date', 'value' => data_to_db2($ricerca_data), 'compare' => '<=', 'type' => 'NUMERIC'),
array('key' => 'portfolio_end_date', 'value' => data_to_db2($ricerca_data), 'compare' => '>=', 'type' => 'NUMERIC')
)
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
$post_count = wp_count_posts();
while ( $the_query->have_posts() ) {
// DO WHAT YOU WANT
}
}
My advice is to use meta_query in $args array

Exclude a featured post from the Wordpress loop

My Wordpress front page has both a featured article (styled inside a box) and a list of recent posts (styled separately). I want to display these recent posts using Wordpress loop excluding the featured post. It is easy to achieve excluding a certain category or a tag, but in my case I want to exclude a post with a custom field. The featured post has a custom field with a name and a value of: featured = yes.
How do I achieve this without using a plugin?
You can use meta_query parameter, as explained in http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
Something like:
$args = array(
'post_type' => 'any',
'meta_query' => array(
array(
'key' => 'featured',
'value' => 'yes',
'compare' => 'NOT LIKE'
)
)
);
$query = new WP_Query( $args );
$args = array(
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'featured',
'value' => 'yes',
'compare' => '='
),
));
$ids = array();
$query = new WP_Query($args); // fetching posts having featured = yes
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$ids[] = $post->ID; // building array of post ids
}
}
$args = array( 'post__not_in' =>$ids); // excluding featured posts from loop
query_posts($args);
while (have_posts()) : the_post();
// rest of the code

Show post based on the value of another custom field

How do you display posts based on the value of one of its custom fields? My code below doesn't seem to work:
$args = array(
'post_type' => 'sample-cpt',
'meta_query ' => array(array('key'=>'cpt_display', 'value' => 1))
);
$samples = new WP_Query($args);
This still returns all posts even if the value of cpt_display is a 0. Am I missing something?
Try this..
Adding the meta_key = key as part of the args,
then include it again in the meta_query array
$args = array(
'post_type' => 'sample-cpt',
'meta_key' => 'key',
'order' => 'ASC',
'meta_query' => array(
array(
'key' => 'key',
'value' => 1
)
)
);
$samples = new WP_Query($args);
http://codex.wordpress.org/Class_Reference/WP_Query
I found the answer. The value of 'meta_value' needs to be an array not a string. This is a huge deviation from the WP_Query Codex and needs to be fixed.
$args = array(
'post_type' => 'sample-cpt',
'meta_key' => 'cpt_display',
'meta_value' => array(1)
);
$samples = new WP_Query($args);
wp_reset_query();
Now it only shows posts with the custom field value of 1. Looking at the request query, it shows that by using an array, the request gets converted into using the SQL IN() to search for posts instead of '=':
...postmeta.meta_key='cpt_display' AND CAST(postmeta.meta_value AS CHAR) IN ('1'))...
Source: Display custom post type by custom field date range

Resources