I'm using Barcelona theme and I need to get authors who have posted in certain category.
In my author.php template I have:
$barcelona_authors = get_users( array(
'fields' => 'ID',
'who' => 'authors',
'order' => 'DESC',
'orderby'=> 'post_count'
) );
<?php
foreach ( $barcelona_authors as $barcelona_author_id ) {
barcelona_author_box( $barcelona_author_id, false );
}
?>
How to get authors who have posted to category ID 59?
For example I tried with:
$barcelona_authors = get_posts('category=59');
But I'm getting error. Any help?
ERROR:
Notice: Object of class WP_Post could not be converted to int in
/home/wp-includes/author-template.php on line 296
Your get_posts( 'category=59' ) code should work. I have tested the following on one of my test wordpress installation and it works (with a different category ID of course). If you get an error during the get_posts call you need to show us the error.
<?php
$category_posts = get_posts( 'category=59' );
$authors_ids = array();
$authors = array();
foreach( $category_posts as $cat_post ) {
$authors_ids[] = $cat_post->post_author;
}
// Not sure if you need more data than just the ID so here is what you need
// to get other fields.
foreach( $authors_ids as $id ) {
$user = get_user_data( $id );
// Display name: $user->display_name;
// Nice name: $user->user_nicename;
// etc...
}
?>
This was the solution for Barcelona theme:
$barcelona_posts = get_posts('cat=59');
$barcelona_author_ids = array();
foreach ( $barcelona_posts as $k => $v ) {
if ( ! in_array( $v->post_author, $barcelona_author_ids ) ) {
$barcelona_author_ids[] = $v->post_author;
}
}
$barcelona_authors = get_users( array(
'fields' => 'ID',
'who' => 'authors',
'order' => 'DESC',
'orderby'=> 'post_count',
'include' => $barcelona_author_ids
) );
Related
I am trying to query for a string if present in a custom field.
e.g. Within the custom field I have a value like: Milano romani and If I have a string Milano from GET, I should find it. But the following gives me all posts even if no string match in the custom field
<?php
$myTerm = $_GET['cityName'];
$catIds = array();
$args = get_posts(
array(
'post_type' => 'post',
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'usp-custom-23',
'value' => $myTerm,
'compare' => 'LIKE'
)
)
)
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
$id = get_the_ID();
array_push($catIds, $id);
}
$catIds = implode( ", ", $catIds );
if(count($catIds) > 0){
$arrayFUllCity = "fullCity";
} else {
$arrayFUllCity = "empty";
}
}
var_dump($catIds);
?>
I am needing some assistance. My goal is to query "clinics" post type by array of zip codes. Then get the ID's of those clinics and run another query of post type called "clinicspromo" to get those results. Then inside the loop, run query #3 to retrieve the clinic information again that is tied to each clinic promo. I have most of it completed, I am just having a problem turning the results of $post->ID; to an array of ID's separated by commas just like the zip code list. Any help would be appreciated!
$zipcodelist = '90001, 90002, 90003';
$args = array(
'orderby' => 'post_title',
'order' => 'DESC',
'post_type' => 'clinics',
'meta_query' => array (
array (
'key' => 'cliniczipcode',
'value' => $zipcodelist,
'compare' => 'IN'
)
) );
$postlist = get_posts( $args );
$posts = array();
foreach ( $postlist as $post ) {
$posts[] += $post->ID;
}
$current = array_search( get_the_ID(), $posts );
$argstwo = array(
'orderby' => 'post_title',
'order' => 'DESC',
'post_type' => 'clinicpromos',
'meta_query' => array (
array (
'meta_key' => 'assignclinic',
'meta_value' => $current,
'compare' => 'IN'
)
) );
$the_query = new WP_Query( $argstwo );
Changed your foreach loop like: I think you have an extra + sign when storing the IDs.
This is your loop:
foreach ( $postlist as $post ){
$posts[] += $post->ID;
}
Replace it with:
foreach ( $postlist as $post ){
$posts[] = $post->ID;
}
Try this, you do not need +
foreach ( $postlist as $post ) {
$posts[] = $post->ID;
}
I am not expert on php and need some help about custom query on my wordpress author page.
I have no problem when using WP_User_Query, the problem is i need to combine it with $curauth
What i need to do is get authors list with WP_User_Query that filtered with current author page as a custom field value.
here is code that got error:
// WP_User_Query arguments
$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
$label = echo $curauth->user_login;
$args = array (
'role' => 'contibutor',
'number' => '10',
'order' => 'ASC',
'orderby' => 'display_name',
'meta_query' => array(
array(
'key' => 'user_label',
'value' => $label,
),
),
);
// The User Query
$user_query = new WP_User_Query( $args );
// The User Loop
if ( ! empty( $user_query->results ) ) {
foreach ( $user_query->results as $user ) {
// do something
}
} else {
// no users found
}
I hope someone here can help to resolve my problem. Thank you
Try this
$curauth = ( isset( $_GET['author_name'] ) ) ? get_user_by( 'slug', $author_name ) : get_userdata( intval( $author ) );
$args = array (
'role' => 'contibutor',
'number' => '10',
'order' => 'ASC',
'orderby' => 'display_name',
'meta_query' => array(
array(
'key' => 'user_label',
'value' => $curauth->user_login,
),
),
);
// The User Query
$user_query = new WP_User_Query( $args );
// The User Loop
if ( ! empty( $user_query->results ) ) {
foreach ( $user_query->results as $user ) {
// do something
}
} else {
// no users found
}
As pointed out by pieter-goosen in the comments, the line $label = echo $curauth->user_login; was causing the error message.
I have been struggling for a couple days with trying to use the data in a custom field to return posts. Here is my function that I have in functions.php. I am able to return my posts, except that they aren't limited to the ones defined in the $json variable. I can decode the json and return the array...but I can't seem to convert it in such a way that it fills in my array properly for the "posts__in" in the $dishes_arg.
Can anyone help me identify what I am doing wrong?
add_action ('woo_loop_before', 'hgf_home_menu');
function hgf_home_menu () {
if (is_home() || is_front_page()) {
wp_reset_query();
global $posts;
$menu_args = array(
'post_type'=>'single_menu',
'posts_per_page' => 1,
'meta_key' => 'orderby_date',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'orderby_date', // Order-By Date field is upcoming
'value' => date("Y-m-d"),
'compare' => '>='
),
array(
'key' => 'orderby_date', // Order-By Date isn't more than two weeks out
'value' => date("Y-m-d", strtotime( "+2 weeks")),
'compare' => '<='
)
),
);
// Get menu that meets criteria
$menus = new WP_Query( $menu_args );
// Show menu that meets criteria
if ( $menus->have_posts() ) {
while ( $menus->have_posts() ) {
$menus->the_post();
}
wp_reset_postdata();
// Get the menu's product/post listing
$json = '[{"id":"435"},{"id":"527"},{"id":"563"},{"id":"568"}]';
$array = json_decode($json);
$include = array();
foreach($array as $a) {
$include[] = $a->ID;
}
$args = array(
'posts_per_page' => -1,
'include' => $include);
$posts = get_posts($args);
$dish_args = array(
'post_type' => 'product',
'post__in' => $posts,
);
// Get dishes in menu listing
$dishes = get_posts( $dish_args );
if ($dishes) {
foreach ($dishes as $dish) {
}
}
} else { // no posts found }
/* Restore original Post Data */
wp_reset_postdata();
}
}
You jSon property is id not ID
$include[] = $a->ID;
Should be
$include[] = $a->ID;
Where/why is there jSon here? Is this coming from something else.
Otherwise that could just be a simple delimitated list or even a normal PHP array.
I figured it out...turns out I just had to cut out a bunch of code that was getting in the way: $include = array() ... $args = array() ... $dish_args = array() ... $dishes = get_posts(). Here is the corrected portion:
$json = '[{"id":"435"},{"id":"527"},{"id":"563"},{"id":"568"}]';
$dishes = json_decode($json);
if ($dishes) {
foreach ($dishes as $dish) {
// Echo titles and permalinks
}
}
The title should give you a pretty good idea about my misadventures. I am working on a project that's made in wordpress and uses WooCommerce, and after a lot of brainstorming and thought about possible compromise, i have reached the point where i am pretty much certain i have to get into the php code to solve the problem conveniently.
The problem is that i have the following website:
As you may have noticed, there is a mash of all the product categories, and what i need to do is split them into 2 main categories: food and drink. I turned what woocommerce can do by its built in functions and i just can't get it to work so i figured i'd have to write my own function. Now if any of you knows that i can actually do it with what i have i'd be happy if somebody told me. If not what i need is to create a function which can actually select all the categories belonging to a parent category or something the likes.
public function product_categories( $atts ) {
global $woocommerce_loop;
extract( shortcode_atts( array (
'number' => null,
'orderby' => 'name',
'order' => 'ASC',
'columns' => '4',
'hide_empty' => 1,
'parent' => ''
), $atts ) );
if ( isset( $atts[ 'ids' ] ) ) {
$ids = explode( ',', $atts[ 'ids' ] );
$ids = array_map( 'trim', $ids );
} else {
$ids = array();
}
$hide_empty = ( $hide_empty == true || $hide_empty == 1 ) ? 1 : 0;
// get terms and workaround WP bug with parents/pad counts
$args = array(
'orderby' => $orderby,
'order' => $order,
'hide_empty' => $hide_empty,
'include' => $ids,
'pad_counts' => true,
'child_of' => $parent
);
$product_categories = get_terms( 'product_cat', $args );
if ( $parent !== "" )
$product_categories = wp_list_filter( $product_categories, array( 'parent' => $parent ) );
if ( $number )
$product_categories = array_slice( $product_categories, 0, $number );
$woocommerce_loop['columns'] = $columns;
ob_start();
// Reset loop/columns globals when starting a new loop
$woocommerce_loop['loop'] = $woocommerce_loop['column'] = '';
if ( $product_categories ) {
woocommerce_product_loop_start();
foreach ( $product_categories as $category ) {
woocommerce_get_template( 'content-product_cat.php', array(
'category' => $category
) );
}
woocommerce_product_loop_end();
}
woocommerce_reset_loop();
return '<div class="woocommerce">' . ob_get_clean() . '</div>';
}
This i identified as the menacing WooCommerce function that does not behave. Help. Please help me :(
What about using WooCommerce's built-in [product_categories] shortcode? You could pass categories you want through the id="" attribute.