Get wordpress comments without trackbacks? - php

I can get the wordpress comments list using get_comments.
For example:
$comments = get_comments('status=approve&number=10');
It returns the trackbacks also. Is it possible to get only human comments (without trackbacks etc)? Thanks.

I believe:
$comments = get_comments('status=approve&number=10&type=comment');
Check out the documentation reference, though in the case of get_comments() it's not particularly helpful.
Also, another possible syntax which I find cleaner, is that the above syntax is equivalent to:
$args = array(
'status' => 'approve',
'number' => 10,
'type' => 'comment',
);
$comments = get_comments( $args );
The $args array defines the constraints you want when getting the comments.

Related

query_posts with custom taxonomy

This is to create a custom RSS feed. I have a CPT called quotes with a Taxonomy called quote_category
I want all posts where quote_category = 'Dogs' which has ID = 115. This isn't working:
$postCount = 10; // The number of posts to show in the feed
$postType = 'quotes'; // post type to display in the feed
$catName = 'Dogs';
$posts = query_posts(array('post_type'=>$postType, 'quote_category'=>$catName, 'showposts' => $postCount));
Generally speaking the consensus is to avoid query_posts at all costs. The official documentation even backs this up:
Note: This function will completely override the main query and isn’t intended for use by plugins or themes. Its overly-simplistic approach to modifying the main query can be problematic and should be avoided wherever possible. In most cases, there are better, more performant options for modifying the main query such as via the ‘pre_get_posts’ action within WP_Query.
Instead, get_posts() will do everything that you want it to do. For your case, you just want to add on a tax_query with your information.
$posts = get_posts(
[
'post_type' => 'quotes',
'numberposts' => 10,
'tax_query' => [
[
'taxonomy' => 'quote_category',
'field' => 'name',
'terms' => 'Dogs',
],
],
]
)
The terms parameter also accepts an array if you need multiple:
'terms' => ['Dogs', 'Cats'],
Edit
If you are using get_template_part then you are probably also using the standard WordPress loop, too. That is actually one of the few times that you could use query_posts but I still personally just don't use it nor recommend it. In an ideal world, you'd probably use a filter just as pre_get_posts but I unfortunately I don't have time to write code to test that. You should be able to use the below code.
This code is an action that calls an anonymous function which adds a feed using another anonymous function as a callback. Your code does the exact same thing, however you are using named functions which is 100% fine, safe and common to do. I just personally prefer to keep my hooks and logic all together and not chase names down. Once again, totally personal preference.
If you still receive an internal error, make sure that both WordPress and PHP debugging is enabled so that you can see what the actual error is, and it is probably a little typo on my part.
add_action(
'init',
static function () {
add_feed(
'quotes',
static function () {
// Override the main query since the render functions wants to use the loop
global $wp_query;
$wp_query = new WP_Query(
[
'post_type' => 'quotes',
'post_count' => 10,
'tax_query' => [
[
'taxonomy' => 'quote_category',
'field' => 'name',
'terms' => 'Dogs',
],
],
]
);
get_template_part('rss', 'quotes');
}
);
}
);

Filter by 'code' using odoo product API

I'm trying to filter by a specific product code using odoo's PHP api. If I filter by qty_available > 0 it works fine:
$records = $models->execute_kw($db, $uid, $pass, 'product.product', 'search_read', array(
array(
array('qty_available', '>', 0),
),
), array(
'fields' => array('code', 'display_name', 'qty_available')
));
... but when I filter by code, it returns all results, essentially ignoring any filters:
$records = $models->execute_kw($db, $uid, $pass, 'product.product', 'search_read', array(
array(
array('code', '=', 'T-0001'),
),
), array(
'fields' => array('code', 'display_name', 'qty_available')
));
Does anyone know why this may be happening please? It also works fine using an integer ID, just not the string code. Thanks!
In the model product.product code field is the functional field/compute field .
These type of field are Not Searchable by default.
But you can mark the store=True , and search it.
NOTE:There are pros as well as con's of marking the store=True
Thanks to Prakash Sharma for his help, but it turns out out that the actual way to get it to work was to use product.product's field default_code rather than code, which fortunately is searchable. This may be different depending on others' setups, but I wasn't willing to go in and change the database and I think the internal reference should be kept pure as it was designed.

Why won't meta_value recognize the numeric value "1"?

I am currently working a magazine website that consists of multiple issues and various types of articles. I assigned a variable for the custom field issue number, and included it in my query in order to display only posts that are in the issue of the current page. Here's the query.
$args = array(
'post_type' => array('story','letter','interview'),
'meta_query' => array(
array(
'value' => "$issue_number",
),
),
);
$my_query = new WP_Query( $args );
This works for all issues except the first one. For some reason, my query will not recognize the numeric value "1", so I have to literally spell out "one" in the custom field for all articles that belong in the first issue.
Your meta_query is incomplete. You need to specify a key and since it's a numerical value you have to set the type. You'll also want to remove the quotes around the value.
'meta_query' => array(
array(
'key' => '_my_issue_number_key',
'value' => $issue_number,
'type' => 'NUMERIC',
),
),
Keep in mind that the issue may not be in your query but in the way you're saving the value. It would be very easy for the value '1' to be confused with 'true'. If you're still having trouble after making the changes above, open up your postmeta table and check the value being stored.

Check if string exists in meta key string using meta_query check in wordpress get_posts function

OK you would think this is a fairly trivial piece of code, but it is not working.
The code below is not broken, it is just not working as it should.
Ok, in wordpress I have a custom field in my custom post-type: individual, this custom field has a meta_key of: login_email
The contents of this meta_key is simply email addresses.
Here are some examples of the contents..
bob.dougal#gmail.com, bob.dougal#company.com, bob.dougal#hotmail.com
or..
bob.dougal#gmail.com, bob.dougal#hotmail.com
or even just a single email string..
bob.dougal#hotmail.com
OK, so now you seen the string contents of the custom field. I can explain what I am trying to achieve.
I have this variable... $current_user_email = $current_user->user_login;
Which is a single email address string.
I then need to find if this email address exists within the meta_key contents. So this how I've done it...
$lastposts = get_posts(array(
'posts_per_page' => 1,
'post_type' => 'individual',
'post_status' => 'private',
'meta_query' => array(
array(
'key' => 'login_email',
'value' => $current_user_email,
'compare' => 'IN'
)
)
));
In my example above, if I echo $current_user_email it outputs bob.dougal#company.com
But even though bob.dougal#company.com exists in one of the custom fields like this... bob.dougal#gmail.com, bob.dougal#company.com, bob.dougal#hotmail.com, my get_post returns nothing.
If I then go to the post editor and remove all the other emails from the custom field so bob.dougal#company.com is the only text in the custom field, then the above query works!
My question is, how can I get meta_query to find bob.dougal#company.com within a meta_key which contains this string: bob.dougal#gmail.com, bob.dougal#company.com, bob.dougal#hotmail.com
Because 'IN' is not doing what it is meant to.
Code taken from http://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
Thanks
I believe this may be happening because you are storing multiple email addresses in one key, as opposed to having multiple keys with one email address each.
Because of this, IN will not work as it checks if a given value exists within a set of possibilities (similar to how the PHP in_array() works).
What you need to do is replace IN with LIKE. This will check the whole string for occurrences of your email address and return matches.
You should be careful though, as this can lead to inconstant results. For example, if $current_user_email = bob.dougal#company.com; then all of the following will be true -
bob.dougal#gmail.com, 1bob.dougal#company.com, bob.dougal#hotmail.com
bob.dougal#gmail.com, 12bob.dougal#company.com, bob.dougal#hotmail.com
bob.dougal#gmail.com, 123bob.dougal#company.com, bob.dougal#hotmail.com
I'll grant you that it is unlikely, but that's why you should not use strings to store multiple values. I'd strongly suggest using multiple keys (you can have many with the same name) and then you can use IN in your query. This would also guarentee only exact matches.
$current_user_email is a string. So transform this string into an array:
$current_user_email = explode(", ", $current_user_email);
$lastposts = get_posts(array(
'posts_per_page' => 1,
'post_type' => 'individual',
'post_status' => 'private',
'meta_query' => array(
array(
'key' => 'login_email',
'value' => $current_user_email,
'compare' => 'IN'
)
)
));

Elgg Filter which counts answers and likes

I try developing two filter which order a list of groupforumtopics by the amount of answers and the amount of likes. I know the function: countAnnotations('likes') and countComments() but i have no idea how to use them. I use a switch case and depending on the input i create the $options array.
$options = array(
'type' => 'object',
'subtype' => 'groupforumtopic',
'limit' => 10,
'container_guid' => $guid,
'full_view' => FALSE,
);
$content = elgg_list_entities($options);
Does someone have a tip or better a solution?
Elgg likes and replies are annotations. So you have to use elgg_list_entities_from_annotation_calculation(). Its better to ask these type of questions in the elgg community forum, so that it will be helpful for others too.

Categories