I'm using WordPress 3.0.1 and want to order the comments from a post using a rating custom field.
Is this possible? I'm already using the callback property from wp_list_comments to customize the appearance of the comments.
Unfortunately this way I can only access the comments one by one and can't affect the order of the all result array.
I've already have a table with all the votes from the users.
Thanks in advance.
// get comments of post 1234
$comments = get_comments( array('post_id' => 1234) );
// ... order your comments collection using php (eg. usort) here ...
// print your comments
wp_list_comments( array( 'callback' => 'woocommerce_comments' ), $comments);
Try $comments = get_comments('postId=x');. It should be indexed by comment id. You can then look up the comment rating in your table.
Related
I'm trying to make the comments list in wordpress posts so that they will only list comments with a certain meta_key and meta_value. So far, I've tried finding answers on google and only found out how to assign a meta key/value when someone adds a comment in the comments form.
in the theme's functions.php
<?php
add_action( 'comment_post', function($comment_id) {
add_comment_meta( $comment_id, $my_meta_key, $my_meta_value );
});
Now what I can't figure out is, how do I make the wordpress posts filter the comments so they only list comments with the certain meta key and value I want them to.
I've made my theme using underscores and I list the comments inside comments.php using the wp_list_comments() function.
Here's an example how to modify the main comment query, in comments_template(), through the comments_template_query_args filter (available for WordPress versions 4.5+):
/**
* Filter non-threaded comments by comment meta, in the (main) comments template
*/
add_filter( 'comments_template_query_args', function( $comment_args )
{
// Only modify non-threaded comments
if( isset( $comment_args['hierarchical'] )
&& 'threaded' === $comment_args['hierarchical']
)
return $comment_args;
// Our modifications
$comment_args['meta_query'] = [
[
'key' => 'some_key',
'value' => 'some_value',
'compare' => '='
]
];
return $comment_args;
} );
where you adjust the meta query to your needs. Note that this example assumes PHP 5.4+.
So, I have a custom field called "geo_location".
And under that field, there are total 5 different values (1 to 5).
Now, there are total 50 posts that have one of the five values.
For example, post#1 has geo_location="1" and post#2 has geo_location="3" etc.
Is there a way to show all the posts under the same 'geo_location' value?
So for example, I want to show all the posts under the same geo_location="1".
how can I achieve this?
Thank you.
You need to use WP_Query to create a database query using your custom meta-data.
This is done like this:
$args = array(
'meta_key' => 'geo_location',
'meta_value' => '1'
);
$query = new WP_Query( $args );
Then you can output the result using the loop in the usual way.
According to the WordPress codex, the get_categories() method accepts the following arguments for its orderby property:
**orderby** (string) Sort categories alphabetically or by unique category ID. The default is sort by Category ID. Valid values:
id
name - default
slug
count
term_group
However, taking a look into the "wp_term_relationships" table there is a seemingly unused field called "term_order" which, for every category I've ever created is set to 0.
Is it possible to use the term_order field in order to serve as an indexed sort order for categories?
I've placed incremental values into this field for my categories and I'm trying to pass the order to the function with the code below to no avail:
$cat_args=array(
'hierarchical' => 0,
'orderby' => 'term_order',
);
$categories = get_categories($cat_args);
Many years later,
You can add this code in your functions.php :
function wpcf_filter_terms_order( $orderby, $query_vars, $taxonomies ) {
return $query_vars['orderby'] == 'term_order' ? 'term_order' : $orderby;
}
add_filter( 'get_terms_orderby', 'wpcf_filter_terms_order', 10, 3 );
This code force WP to use the orderby => term_order argument in your term query.
The answer by #cédric-dagherir-dahive which suggests using get_terms_orderby filter is a great approach to display terms in the order they were added to the object (post). However, it does not work for all terms queries, and would generate the following error:
Unknown column 'term_order' in 'order clause'.
Because simply, some terms queries are only fetching data from (wp_terms & wp_term_taxonomy) database tables, and do not have any INNER JOIN with (wp_term_relationships) table, which has the term_order field. Hence the error message.
The order by term_order clause should only be added if the query is concerned about the many-to-many relationship between the taxonomy and the object (e.g. get_the_terms).
Therefore, I updated the code to firstly check if the term query involves objects before adding the term_order to the order clause:
function wpcf_filter_terms_order( $orderby, $query_vars, $taxonomies ) {
return ( ! is_null($query_vars["object_ids"]) ) ? 'term_order' : $orderby;
}
add_filter( 'get_terms_orderby', 'wpcf_filter_terms_order', 10, 3 );
I've searched all the wordpress functions and the only one you can use with 'term_order' is wp_get_object_terms and you can see its function here.From this point on, even if wordpress doesn't use this to filter the categories in get_categories() you or other theme/plugin developers can use the above function to get object_terms and to order/filter them by term_order.
A few more years later I found this solution:
The task can be solved with a filter and an adapted taxonomy.
https://core.trac.wordpress.org/ticket/5857#comment:4
UPDATE: I have tried using the following code:
<?php if (is_category(events)) {
$posts = query_posts($query_string . '&orderby=event_date&order=desc');
} else {
$posts = query_posts($query_string . '&orderby=title&order=asc');
}
?>
Is there any reason why that wouldnt work? It seems to work fine organising posts in alphabetical order, but still no luck on the date order within 'events'.
--
After searching through various existing questions I can't quite find a solution to what I am trying to do.
Currently all posts on my site are ordered alphabetically, which is fine except for one new category that I have added. For this category I want to order all posts by a value that I enter into a custom field. The field is called 'event_date' - so I want to order the posts by date essentially, but not the date the post was created, the date the user manually enters into this field.
I managed to get it working by using:
<?php if (is_category($events)) { $posts = query_posts($query_string . '&orderby=$event_date&order=asc'); } ?>
However this overrides the aphabetical order for all other pages.
For alphabetical order I am using:
<?php if (is_category()) { $posts = query_posts( $query_string . '&orderby=title&order=asc' ); } ?>
Essentially I want a statement that tells the page to order all posts in aphabetical order, unless the category is 'events', where I want to order them by the custom event date.
As you can probably tell I'm very much front end, not back end so a lot of this is fairly new to me, so any help or advice is appreciated.
To order posts by a custom field value, you need add the custom meta field to the query itself. orderby=meta_value in addition to meta_key=metafieldid will allow ordering in this fashion.
I would use the pre_get_posts hook and modify the query object if get_query_var( "cat" ) (or a similar query var) returns the desired post category.
add_action( "pre_get_posts", "custom_event_post_order" );
function custom_event_post_order( $query )
{
$queried_category = $query -> get_query_var( "cat" );
/*
* If the query in question is the template's main query and
* the category ID matches. You can remove the "is_main_query()"
* check if you need to have every single query overridden on
* a page (e.g. secondary queries, internal queries, etc.).
*/
if ( $query -> is_main_query() && $queried_category == 123 )
{
$query -> set( "meta_key", "event_date" ); // Your custom field ID.
$query -> set( "orderby", "meta_value" ); // Or "meta_value_num".
$query -> set( "order", "ASC" ); // Or "DESC".
}
}
Remember that this approach overrides all queries that are using the category in question. You can build custom WP_Query objects that use their own parameters for constructing loops.
You also should standardize the way you save the custom field data to the database. For dates I prefer using UNIX-timestamp formatted times that are easy to move around and manipulate. This way no accidents happen when querying and some data is formatted in another way that the rest is.
Disclaimer: I did not have the time to test the above code in action, but the general idea should work properly.
EDIT: of course the above code should be inserted to functions.php or a similar generic functions file.
What about:
<?php $posts = query_posts($query_string . (is_category($events)?'&orderby='.$event_date:'&orderby=title') . '&order=asc'); ?>
<?php
$recent = new WP_Query(“cat=ID&showposts=x”);
while($recent->have_posts()) : $recent->the_post();
?>
Hopefully I understood your question, use the WP_Query and within the orderby add a space between your order by columns:
$args = array(
'posts_per_page' => 100,
'orderby' => 'title',
'order' => 'ASC',
);
if(is_category($events)){
$args['orderby'] .= " meta_value_num";
$args['meta_key'] = 'event_date';
}
$posts = (array) new WP_Query( $args );
I'm attempting to build a small tool with PHP to import content from my current CMS into Drupal 7 because I have about 10k+ articles to bring in. So far I've gotten the title, summary, body, author and published date to come through, but when it comes to categories (tags), I am completely baffled.
Each of my current categories/tags are stored in a database table, each having their own ID, name and description. I can pull this out per article and sort it however I'd like (string, array, etc).
During my import, I'm guessing I should do something like this:
$node->field_tags = array(
'und' => array(
array(
'Update',
'News',
'Report'
)
)
);
I've also tried:
$node->field_tags = array(
'Update',
'News',
'Report'
);
But these nor feeding in a comma separated string of words doesn't work. The Drupal 7 API documentation doesn't seem to explain this anywhere that I've found.
What's the format for sending tags through or what's the documentation page I haven't been able to locate? Thank you in advance!
Term fields in Drupal 7 are related to physical taxonomy terms, so you'll need to create a term for each category, and then add that reference as the field's value.
This code might help:
// Load the tags vocabulary
$vocab = taxonomy_vocabulary_machine_name_load('tags');
$term = new stdClass;
$term->vid = $vocab->vid; // Attach the vocab id to the new term
$term->name = 'Category Name'; // Give the term a name
taxonomy_term_save($term); // Save it
// Add the tags field
$node->field_tags = array(
LANGUAGE_NONE => array(
'tid' => $term->tid // Relate the field to the new category term using it's tid (term id)
)
);