In WordPress with BuddyPress, how can I display for a user posts (custom post type) that are created by his friends only?
Not as/in activity steam (it's disabled on the site), but as usual loop while(have_posts()):.
Thank you in advance.
Try:
$friend_ids = friends_get_friend_user_ids( bp_loggedin_user_id() );
if ( ! empty( $friend_ids ) {
$args = array(
'author__in' => $friend_ids
);
$query = new WP_Query( $args );
}
Adjust $args to include other parameters like post_type. And then create a while loop.
Related
First sorry for my english, is not my first language.
So what I want to do is to get the products of a particular page, using the get variable, so this is my current code...
$page = 0;
if(isset($_GET['pagina'])){
$page = $_GET['pagina'];
}
$args = array( 'post_type' => 'product','posts_per_page' => 1, 'page' => $page);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
echo get_the_title();
endwhile;
I though the parameter 'page' => $page will do exactly that... but it seems I was wrong and maybe I understood it wrong when I was reading it.
So example if I have 2 products: product_1 and product_2 and my posts_per_page is 1 what Im trying to archive is that
if I type http://myurl.com/?pagina=1 will show product_1
and if I type http://myurl.com/?pagina=2 will show product_2
Thanks in advance
EDIT: I think my english failed to communicate my problem so ima try explaning in other example
What I want is to get the products that suppose to go in the page I get tru $_GET
So if I have to use my own pagination, it will look something like this:
$page = 1;
$posts_per_page = 1;
if(isset($_GET['pagina'])){
$page = $_GET['pagina'];
}
$start_from = $posts_per_page * ($page-1);
$sql = "SELECT * FROM Products LIMIT $start_from,$posts_per_page"
I'm having a lot of trouble getting the product SKU for a product in a single product page inside of functions.php. I have multiple single product pages and I want different text to display depending on the product. I've created a child theme and I'm working in the functions.php file. I'm new to wordpress and editing themes so I don't quite understand the order of operations yet. I was able to get code working to loop through and give me all the SKU's for all the products but that's independent of the actual page that I'm on.
I've tried a bunch of things. The common solution seems to be:
global $product;
echo $product->get_sku();
but that doesn't work. For some reason the $product variable is null inside the functions.php script.
The loop that I have loops through all the product posts and gets the post ID. I also tried getting the ID of the current page but was also unsuccessful in doing that (the code below was copied from another site). Any help would be greatly appreciated. Thanks.
$full_product_list = array();
$loop = new WP_Query( array( 'post_type' => array('product', 'product_variation'), 'posts_per_page' => -1 ) );
while ( $loop->have_posts() ) : $loop->the_post();
$theid = get_the_ID();
$product = new WC_Product($theid);
get_sku() works just fine assuming that your theme has not removed woocommerce_after_single_product_summary for any reason.
add_action( 'woocommerce_after_single_product_summary', 'so_32545673_add_sku', 35 );
function so_32545673_add_sku(){
global $product;
if( $product->get_sku() ) {
echo 'the sku = ' . $product->get_sku();
}
}
I didn't see the output at first because it was below all the related products and upsells, etc.
Additionally you may want to switch to wc_get_product() instead of trying to call the class directly. With the following I get a list of product SKUs. You should use an if( $loop->have_posts() ) to open the <ul> but I'm being lazy.
$loop = new WP_Query( array( 'post_type' => array('product', 'product_variation'), 'posts_per_page' => -1 ) );
while ( $loop->have_posts() ) : $loop->the_post();
$theid = get_the_ID();
$product = wc_get_product($theid);
if( $product->get_sku() ) echo '<li>' . $product->get_sku() . '</li>';
endwhile;
In a plugin for displaying recent posts in your sidebar widget, how can we apply a filter to the plugin's functions.php so that it won't include the current page/post in the display?
The plugin author replied, before he entered a long silence: "You can add custom parameter to the rpwe_default_query_arguments filter. Just add exclude => get_the_ID() to the filter."
Is it here, that we add it?
// Allow plugins/themes developer to filter the default query.
$query = apply_filters( 'rpwe_default_query_arguments', $query );
How?
This is the plugin: https://wordpress.org/plugins/recent-posts-widget-extended/
I found some guidance that appears to be quite simple
but then it results in errors in my site (localhost) while trying to correct the syntax. => seems to be not correctly used.
This is what I have so far:
add_filter( 'rpwe_default_query_arguments', 'rpwe_exclude_current' );
function rpwe_exclude_current ( $query ) {
'exclude' => get_the_ID()
$posts = new WP_Query( $query );
return $posts;
}
Here is the answer that worked in my situation:
add_filter( 'rpwe_default_query_arguments', 'my_function_name' );
function my_function_name( $args ) {
if( is_singular() && !isset( $args['post__in'] ) )
$args['post__not_in'] = array( get_the_ID() );
return $args;
}
Here is the site where I found it.
I set up a plugin that adds a custom post type and then brings in a bunch of dummy content with wp_insert_post on activation like so:
register_activation_hook( __FILE__, array( $this, 'activate' ) );
public function activate( $network_wide ) {
include 'dummycontent.php';
foreach ($add_posts_array as $post){
wp_insert_post( $post );
};
} // end activate
I would like to remove this content when the plugin is deactivated so I set up this function:
register_deactivation_hook( __FILE__, array( $this, 'deactivate' ) );
public function deactivate( $network_wide ) {
include 'dummycontent.php';
foreach($remove_posts_array as $array){
$page_name = $array["post_title"];
global $wpdb;
$page_name_id = $wpdb->get_results("SELECT ID FROM " . $wpdb->base_prefix . "posts WHERE post_title = '". $page_name ."'");
foreach($page_name_id as $page_name_id){
$page_name_id = $page_name_id->ID;
wp_delete_post( $page_name_id, true );
};
};
} // end deactivate
It works just fine. Except because the custom post type is created with the same plugin that these two functions are run through, the post type is removed before the posts themselves can be through wp_delete_post. When I test these functions out without the custom post type posts are added upon activation and removed upon deactivation. So I know the problem is with the post type. Does anyone know how to work around this?
Try something like this (YOUTPOSTTYPE is the name of your post type):
function deactivate () {
$args = array (
'post_type' => 'YOURPOSTTYPE',
'nopaging' => true
);
$query = new WP_Query ($args);
while ($query->have_posts ()) {
$query->the_post ();
$id = get_the_ID ();
wp_delete_post ($id, true);
}
wp_reset_postdata ();
}
It works in my plugin, it should works in your's. (This has been tested with WordPress 3.5.1).
wp_delete_post($ID, false) sends it to Trash. Only when you remove from Trash is a post really deleted. That's why it works with $force = true.
So it works as expected. First posts go to Trash, then they get actually deleted. Like Recycle Bin. Trace the post_status change to see when it hits the Trash if you want to do anything then. Otherwise wait for the delete.
Also delete content on uninstall and not on deactivate. Consider deactivating a plugin as pausing it and uninstalling it when you really want it gone.
Try this Function
function deactivate () {
$args = array(
'post_type' => 'POST_TYPE',
'posts_per_page' => - 1
);
if ( $posts = get_posts( $args ) ) {
foreach ( $posts as $post ) {
wp_delete_post( $post->ID, true );
}
}
}
I have a custom field, say, "mood", and I need to display list of tags for all posts that have "mood" = "grumpy". Is it possible to do this without fetching all posts and then fetching tags for each of them?
You can use the function get_posts();
$args = array(
'meta_key' => 'mood',
'meta_value' => 'grumpy',
);
$your_posts = get_posts( $args );
Untested, but this should achieve what you want. Just look out for typos and missed semi-colons!
While you don't get the whole post, you do still have to query the database for the ID of posts with 'mood' = 'grumpy', so unless you have lots of posts, it's probably easier to just go with the answer #Dorel gave.
$query = $wpdb->prepare('
SELECT ID
FROM %1$s
LEFT JOIN %2$s
ON %1$s.ID = %2$s.post_id
WHERE %2$s.meta_key = "mood"
AND %2$s.meta_value = "grumpy"
', $wpdb->posts, $wpdb->postmeta
);
$ids = $wpdb->get_col($query);
if(!empty($ids)) : foreach($ids as $post_id) :
$tags = wp_get_post_tags($post_id, $args);
if(!empty($ids)) : foreach($tags as $tag) :
$tags[] = $tag->name;
endforeach;
endif;
endforeach;
endif;
// Now you have an array of Tag names, output them as you wish
Codex for wp_get_post_tags = http://codex.wordpress.org/Function_Reference/wp_get_post_tags
Codex for wp_get_object_terms (to see what $args are available for wp_get_post_tags) =
http://codex.wordpress.org/Function_Reference/wp_get_object_terms#Argument_Options