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
Related
I have a custom post type with its own custom template, and I want to display blog posts that are related to the custom post type title. However, I want to display a referenced post first, then search for any posts with tags, and then find relevant posts after that. Any one of these could result in no results or many. The total number of posts I want to display is 6.
`
$searchtag = strtolower(get_the_title());
$arg1 = array(
'post_type' => 'post',
'p' => $post_id,
);
$arg2 = array(
'post_type' => 'post',
'tag' => $searchtag,
'post_status' => 'publish',
'orderby' => 'post_date',
'order' => 'desc',
'posts_per_page' => 6,
);
$arg3 = array(
'post_type' => 'post',
's' => $searchtag,
'post_status' => 'publish',
'orderby' => 'relevance',
'posts_per_page' => 6,
);
// Get the posts
$query1 = new wp_query( $arg1 );
$query2 = new wp_query( $arg2 );
$query3 = new wp_query( $arg3 );
$related_query = new wp_query();
// Merge the unique posts
$related_query->posts = array_merge($query1->posts, $query2->posts, $query3->posts);
$related_query->post_count = $query1->post_count + $query2->post_count + $query3->post_count;
if($related_query->have_posts):
`
I read an article that stated that using post__not_in was taxing on the database and I should not incorporate it.
Do I need to add logic if the query->post_count = 0?
How can I maintain the order but ensure that there are no duplicates displayed?
I get duplicates in these results currently. I'd like to eliminate them while keeping the order of the posts by query1, then query2 then query3... displaying the first 6 posts.
The better way is make 3 queries with parameter:
'fields' => 'ids'
and there:
$postIds1 = get_posts($args1);
when merge got data:
$postIds = array_merge(array(0), $postIds1, $postIds2, $postIds3);
and when make forth request with
'post__in' => $postIds
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);
I am trying to get specific product using meta key value. Any help would be highly appreciated.
I am trying to display a specific product when the product code is passed via a shortcode.
I have setup metabox and confirmed that the items have custom meta values with key "neproductinfo-ne_item_code"
$atts = shortcode_atts(
array(
'itemcode' => '',
),
$atts, 'products_catalog'
);
$woocommerce_loop['columns'] = 1;
$meta_query_args = array(
array(
'key' => 'neproductinfo-ne_item_code',
'value' => $atts['itemcode'],
'compare' => '='
)
);
$meta_query = new WP_Meta_Query( $meta_query_args );
$products = new WP_Query( array (
'post_type' => 'product',
'post_status' => 'publish',
//'posts_per_page' => 1,
'meta_query' => $meta_query
));
I am getting list of all products regardless of any itemcode passed through shortcode
shortcode example
[products_catalog itemcode='1001']
No if condition found where you are checking the attribute value exists or not.
Based on existence of attribute value, you need to pass meta query. If there is no value then y you passs the meta_query to WP_Query ?
I've added a postmeta (popular_posts) see image below. But when I query posts with meta key "popular_posts" like in below I've had no result:
new WP_Query(array( 'meta_key'=>'popular_posts' ))
Some one can explain me how to properly retrieve that have meta key "popular_posts" ?
This is the simple way to get post by their meta.
$myquery = new WP_Query( "post_type=post&meta_key=popular_posts");
Or You can use this :
$second_loop = get_posts( array(
'meta_key' => 'popular_posts',
'meta_value !=' => '',
) );
$popular_posts_args = array(
'post_type' => 'post',
'orderby' => 'meta_value',
'order' => 'DESC',
'meta_query' => array(
'meta_value' => array(
'key' => 'popular_posts',
'type' => 'NUMERIC'
)));
$popular_posts = new WP_Query($popular_posts_args);
As your meta key store numeric value. It is better to define the type in the argument. Then you can loop through $popular_posts
This question seems to be unanswered on the internet, perhaps because it is not possible. I simply want to query all posts where the repeater field 'has rows'. i.e.
$args = array(
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'repeater_field',
'value' => '',
'compare' => '=!'
)
);
I know an alternative is to run if statements inside the loop to filter them out, but that messes with some other logic which is based off of the number of posts in the query.
Does anyone have any thoughts on this?
Let's consider you have a Repeater Field labeled My Repeater Field and this repeater contains at least one field labeled A Field on the Repeater.
Assuming you have the default wp_ table prefix, you need to look at the DB's wp_postmeta table to notice that values for this field on the repeater are stored with the meta_key:
NAME_OF_THE_REPEATER_index_NAME_OF_FIELD_ON_THE_REPEATER
So, in our case, if a post has 3 rows on the repeater field, its values will be stored as:
my_repeater_field_0_a_field_on_the_repeater
my_repeater_field_1_a_field_on_the_repeater
my_repeater_field_2_a_field_on_the_repeater
Knowing this, if you want to query all posts having at least ONE row on the repeater, you could do:
$meta_query = [
[
'key' => 'my_repeater_field_0_a_field_on_the_repeater',
'compare' => 'EXISTS',
]
];
$args = [
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC',
'meta_query' => $meta_query,
'post_type' => 'post',
'post_status' => 'publish',
];
$posts_array = get_posts( $args );
Note: As stated on WP Docs, you can only use the EXISTS comparison on WP >= 3.5 and you don't need to specify a value when using the 'EXISTS' or 'NOT EXISTS' comparisons in WordPress 3.9 and up. I'm also assuming you are using PHP >= 5.4 so you can use short array syntax. If not, just replace [] for array().
You can query the wordpress database using the $wpdb object. ACF fields are saved in prod_postmeta on the database so that is where you will run your query. Your meta_value will be the key of your repeater field, so make sure you replace that in the query below. All keys for any ACF field with start out with field_ and then random characters/digits will follow like seen below. Then once you have the post id, you can run get_post() on those post ids. Let me know if you need anything else or have questions.
global $wpdb;
$results = $wpdb->get_results("SELECT post_id from prod_postmeta WHERE meta_value = 'field_534eeaaa74199'");
$echo $results;
This works. I have test it. Only by the "Welcome World" post it doesn't work.
$args = array(
'post_type'=> 'post',
'posts_per_page' => -1,
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'repeater_field',
'value' => '0',
'compare' => '!='
)
));
$the_query = new WP_Query( $args );
The "repeater_field" is the name of the field, not the field_key. Here is the count of the rows.