I am trying to run a PHP get_posts query in WordPress to target posts with a meta data key that CONTAINS a certain string. The issue is, I don't know the full key name as it is created independently by another plugin but all keys have in common that they start with "_cegg_data".
Sometimes the field will be called "_cegg_data_ae_googlecom", other times "_cegg_data_ae_youtubecom" and so on.
Is there a way to run a query posts with a meta_query that contains "_cegg_data" in the key (value unknown as well)?
$args = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => '_cegg_data{{anything here}}'
)
)
);
$postslist = get_posts( $args );
You can add a LIKE comparison for partial string macthing.
$args = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => '_cegg_data',
'compare' => 'LIKE',
'value' => '_cegg_data%'
)
)
);
$postslist = get_posts($args);
The % will work as a wildcard matching any number of characters, so this will get every product post where the key starts with _cegg_data.
Related
I was looking on another question , and thought of changing the original query to compare two meta_key values.
This question deals with finding WC coupons that reached their usage limit.
There are two values stored as post meta data: usage_limit and usage_count.
I want to find all the coupons that the usage_count is equal or greater than the usage_limit. Something like:
$args = array(
'posts_per_page' => -1,
'post_type' => 'shop_coupon',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'usage_count',
'value' => [meta_key='usage_limit'] value here,
'compare' => '>='
)
)
);
I didn't find any solution for this type of query, but thought there might be an elegant way.
I'm customizing the admin of wordpress and I've created a new custom input field for the tags of a post.
It's a dropdown list with all the categories of a post. Now, I wanna find all the tags with an specific category. So, I'm using this:
$args = array(
'meta_key' => 'project',
'meta_value' => $idProject,
);
$allTags = get_tags( $args );
Is this the right way to get all the tags that I want? The problem is that the array list with the tags is coming with only one result. It should bring 2 tags with the project id that I'm passing on the variable $args.
Am I missing something?
I found a solution:
$args = array(
'hide_empty' => false, // also retrieve terms which are not used yet
'meta_query' => array(
array(
'key' => 'project',
'value' => $idProject
)
)
);
$terms = get_terms( 'post_tag', $args );
i have two categories 'A' and 'B'. Both have 5 posts(each) with tag 'C'. How i can display the number of posts as 5 in Category 'A' or 'B' page. It showing total number of post with tag 'C' as 10. I need to show it as 5. How i can pass category variable to display exact count with tag within specific category. Here is my current code:
$tag = get_term_by('name', $tags,'post_tag');
$count = $tag->count;
reference: https://codex.wordpress.org/Function_Reference/get_term_by
This is not possible with the get_term_by() method.
Try this:
$args = array(
'posts_per_page' => -1,
'tax_query' => array(
'relation' => 'AND', // only posts that have both taxonomies will return.
array(
'taxonomy' => 'post_tag',
'field' => 'name',
'terms' => 'TAG_C', // you can also use an array with tags.
),
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'CATEGORY_B', // you can also use an array with categories.
),
),
);
$posts = get_posts($args);
$count = count($posts);
I haven't tested this code, but it should work. Ofcourse you do have
to set the correct terms.
UPDATE
When dealing with many posts I would create a $wpdb sql query that counts the rows, so only a number is returned and not all the posts. You can find an sql count example here.
You can use "found_posts" property of the WP_Query class object.
$args = array(
'posts_per_page' => 10,
'category_name' => 'aaa', // category slug
'tag' => 'ccc' // tag slug
);
$wp_query = new WP_Query( $args );
echo "Total posts count: ".$wp_query->found_posts;
I'm using an ACF checkbox to designate categories (called "aims") for Psychology apps/sites. So values would be, say, "Mindfulness", "Well-Being" and "Depression".
Since it's possible to select multiple values, what I'd like to do is have a More Like This where any single post could show other posts that match one or more of the categories of the post.
UPDATE: Here's the code I'm using now, which I'm still not having any luck with:
the_field( 'aims'); /* since I'm on the page for a single post, this gives me the aims for the current post and is currently returning values from the checkbox with multiple selections (as an array, I believe) */
$args = array(
'numberposts' => -1,
'post_type' => 'program',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'aims',
'value' => 'Well-Being', /* I'd actually like this to use the aims for the current post, but I've hard-coded for testing, and am not seeing any results */
'compare' => 'IN')
));
$more_like_this = new WP_Query ($args);
while ($more_like_this->have_posts()) : $more_like_this->the_post();
$star_rating = get_field('overall_score');
?>
<?php the_title(); ?>
<?php echo $star_rating ?>
<?php endwhile;
wp_reset_query();
?>
If you need a wp query to select some posts where multiple ACF checkbox options must be checked you can do like this:
// Get the array with selected options
$fields = get_field('somefield');
// First create the meta_query variable (AND can also be OR)
$meta_query = array('relation' => 'AND');
foreach( $fields as $field ){
$meta_query[] = array(
'key' => 'name_of_acf_checkbox_here',
'value' => $aim,
'compare' => 'LIKE',
);
}
// args
$args = array(
'numberposts' => -1,
'post_type' => 'postype_here',
'meta_query' => $meta_query,
);
$the_query = new WP_Query( $args );
So, I have a search form, that returns custom posts with wp_query. Now I need to find posts with a custom field and specific value: Lets say I have multiple checkboxes named catagories[]. So when I save them it makes a serialized array in db. Now I need to search in the front end posts with lets say value 11 in it. Array looks like that: a:3:{i:1;s:2:"11";i:2;s:2:"33";i:3;s:2:"33";}. So here comes the problem, how can I retrieve information from this serialized array to find all posts with value 11. My code is:
$args = array(
'post_type'=>'paibcresume',
'posts_per_page' => 10,
'paged' => $paged,
'meta_query' => array(),
'tax_query' => array(),
'orderby' => 'date',
'meta_key' => '',
'order' => 'DESC'
);
// BASIC SEARCH FORM
// Search by category
// rbsercategories
// rbwwcategoryhidden
if (isset($_GET['rbsercategories']) && !empty($_GET['rbsercategories'])) {
$args['meta_query'][] = array(
'key' => 'rbwwcategoryhidden',
'value' => $_GET['rbsercategories'],
'compare' => 'IN'
);
}
$the_query = new WP_Query( $args );
while ($the_query->have_posts() ) : $the_query->the_post();
This code works if values in data base are not a serialized array, just simple strings, but doesn't work with arrays so what do I have to do?
Ok, I didn't find the way to search thru serialized arrays, but I found the way to work around it.
So I didn't change any fields in admin, instead I added new with a loop. So I have this fields named rbwwcategoryhidden[], that create this array. I unserialized that array and created new fields for each value:
$wwCategory = get_post_custom_values('rbwwcategoryhidden');
foreach($wwCategory as $wwCategoryValue){$wwCategoryUnser = unserialize($wwCategoryValue);}
$rbWwCatSearchCounter='0';
foreach($wwCategoryUnser as $catId){
$rbWwCatSearchCounter++;
echo '<input type="text" name="rbwwcatforsearch'.$rbWwCatSearchCounter.'" id="rbwwcatforsearch'.$rbWwCatSearchCounter.'" value="'.$catId.'">';
}
So now I got new fields, I personaly have a limit of three fields (user can only check three checkboxes): rbwwcatforsearch1, rbwwcatforsearch2, rbwwcatforsearch3. And I added value to each field from that array: 11, 33 and 33.
And now I just changed the meta_query code in the front end, and it looks like that:
// Search by category
if (isset($_GET['rbsercategories']) && !empty($_GET['rbsercategories'])) {
$args['meta_query'][] = array(
'key' => array('rbwwcatforsearch1','rbwwcatforsearch2','rbwwcatforsearch3'),
'value' => (isset($_GET['rbsercategories'])?$_GET['rbsercategories']:array()),
'compare' => 'IN'
);
}
And it works just perfect :)