Wonder if someone can help; its seems a bit complicated to me;
Baisically I've added this function in my wordpress site, so people can change the author's name;
add_filter( 'the_author', 'guest_author_name' );
add_filter( 'get_the_author_display_name', 'guest_author_name' );
function guest_author_name( $name ) {
global $post;
$author = get_post_meta( $post->ID, 'author_name', true );
if ( $author )
$name = $author;
return $name;
}
But now I want to add some code to show a list of posts from the current posts author but its returning the original author not the author that the top function changed it to; below is the function I'm using to do this, is this possible to change?
function get_related_author_posts() {
global $authordata, $post;
$authors_posts = get_posts( array( 'author' => $author_name->ID, 'post__not_in' => array( $post->ID ), 'posts_per_page' => 3 ) );
$output = '<div class="morepost"><h3>More posts from this author</h3>';
foreach ( $authors_posts as $authors_post ) {
$output .= '<li>' . apply_filters( 'the_title', $authors_post->post_title, $authors_post->ID ) . '</li>';
}
$output .= '</ul></div>';
return $output;
}
then this;
<?php echo get_related_author_posts(); ?>
Know its a bit complicated, if anyone can help that be great
D
This is what Ive come up with so far but could someone show me where I'm going wrong with this code
<?php $author = get_post_meta( $post->ID, 'author_name', true ); $args
= array(
'meta_query' => array (
array(
'key' => 'author_name',
'value' => $author
)
), 'post__not_in' => array( $post->ID ), 'posts_per_page' => 3 ); ?> <?php if ( $wp_query->have_posts() ) : ?> <?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?> DO something
<?php endwhile; ?> NOT POSTS <?php endif; ?>
You need to get post based on the author meta value,
So get the virtual author name first for that post,
$author = get_post_meta( $post->ID, 'author_name', true );
Now get all posts based on this value,
$args = array(
'meta_query' => array(
array(
'key' => 'author_name',
'value' => $author
)
),
'post__not_in' => array( $post->ID )
'posts_per_page' => 3
);
$posts = get_posts($args);
Related
I've been struggling applying filters to my woocommerce shop. I have a website with multiple store pages and I've found a filter to only shop products published within the last 30 days. I want this filter to only apply to the 'new releases' shoppage, not to the whole website. Currently it's filtering everything, so even pages created before last month won't appear for example.
How do I make sure this filter is only applied to 1 store? Can I apply an IF statement and check current URL before the filter works or something?
Any help is appreciated.
Filter: (from https://www.sitepoint.com/community/t/show-latest-30-days-products-in-woocommerce/258276)
function baba_recent_products() {
//return 'This is where the recent products should show up if I get the shortcode working. ';
global $woocommerce_loop;
extract( shortcode_atts( array(
'per_page' => '48',
'columns' => '2',
'orderby' => 'date',
'order' => 'desc'
), $atts ) );
$meta_query = WC()->query->get_meta_query();
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => $per_page,
'orderby' => $orderby,
'order' => $order,
'meta_query' => $meta_query
);
ob_start();
$products = new WP_Query( apply_filters( 'woocommerce_shortcode_products_query', $args, $atts ) );
$woocommerce_loop['columns'] = $columns;
if ( $products->have_posts() ) : ?>
<?php woocommerce_product_loop_start(); ?>
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
<?php endif;
wp_reset_postdata();
return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
}
add_shortcode( 'baba_recent_products', 'baba_recent_products' );
function filter_where($where = '') {
//posts in the last 30 days
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
return $where;
}
add_filter('posts_where', 'filter_where');
function baba_recent_products() {
//return 'This is where the recent products should show up if I get the shortcode working. ';
global $woocommerce_loop;
extract( shortcode_atts( array(
'per_page' => '48',
'columns' => '2',
'orderby' => 'date',
'order' => 'desc'
), $atts ) );
$meta_query = WC()->query->get_meta_query();
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'ignore_sticky_posts' => 1,
'posts_per_page' => $per_page,
'orderby' => $orderby,
'order' => $order,
'meta_query' => $meta_query
);
ob_start();
$products = new WP_Query( apply_filters( 'woocommerce_shortcode_products_query', $args, $atts ) );
$woocommerce_loop['columns'] = $columns;
if ( $products->have_posts() ) : ?>
<?php woocommerce_product_loop_start(); ?>
<?php while ( $products->have_posts() ) : $products->the_post(); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
<?php woocommerce_product_loop_end(); ?>
<?php endif;
wp_reset_postdata();
return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
}
add_shortcode( 'baba_recent_products', 'baba_recent_products' );
function filter_where($where = '') {
global $post;
if($post->ID === /*Page ID as int here*/){
//posts in the last 30 days
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-30 days')) . "'";
}
return $where;
}
add_filter('posts_where', 'filter_where');
I trying to get products by post author id using a WC_Query in WooCommerce, so I tried to include a new custom meta_key "_author", with the following:
add_filter( 'woocommerce_product_data_store_cpt_get_products_query', 'handling_custom_meta_query_keys', 10, 3 );
function handling_custom_meta_query_keys( $wp_query_args, $query_vars ) {
$meta_key = '_author';
if ( ! empty( $query_vars[$meta_key] ) ) {
$wp_query_args['meta_query'][] = array(
'key' => $meta_key,
'value' => esc_attr( $query_vars[$meta_key] ),
'operator' => '==',
);
}
return $wp_query_args;
}
Then I tried to use it with wc_get_products():
$product_list = wc_get_products( array('_author' => get_current_user_id()) );
but it return null array. Any idea?
You can simply get products by author Id in a WC_Query using the undocumented parameter "author'" as follow:
$products = wc_get_products( array(
'status' => 'publish',
'limit' => -1,
'author' => get_current_user_id()
) );
You will get an array of WC_Product Objects
You can use custom query like this
<?php
$authorID = get_the_author_meta('ID');
$args = array(
'post_type' => 'product',
'post_status' => 'publish'
'posts_per_page' => 12,
'product_cat' => 'pants'
'author' => $authorID
);
$loop = new WP_Query( $args );
?>
<div class="author_products">
<?php if ( $loop->have_posts() ) { ?>
<ul class="author_pubproducts">
<?php while ( $loop->have_posts() ) : $loop->the_post();
woocommerce_get_template_part( 'content', 'product' );
endwhile; ?>
</ul>
<?php
} else {
echo __( 'No products found', 'textdomain' );
}
wp_reset_postdata();
?>
Hope it's work
I'm new to creating wordpress shortcodes and have one just about working the way I want. Missing something specific. Currently I am able to put the following on any page - [children] and it pulls a query of all posts from the custom post type "children" I would like to add the option to add the category id within the shortcode - something like [children category="8"] Here is the code I have so far:
add_shortcode( 'children', 'display_custom_post_type' );
function display_custom_post_type(){
$args = array(
'post_type' => 'children',
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
);
$string = '';
$query = new WP_Query( $args );
if( $query->have_posts() ){
while( $query->have_posts() ){
$query->the_post();
$string .= '<div id="childWrapper"><div id="childImage">' . get_the_post_thumbnail() . '</div><div style="clear: both;"></div><div id="childName">' . get_the_title() . '</div><div style="clear: both;"></div></div>';
}
}
wp_reset_postdata();
return $string;
}
Secondary - is it possible for it to show posts in multiple categories, but only where the posts are in each of the categories. For example showing a list of children, who fall under a category for critical care and surgery needed.
Any help would be greatly appreciated.
You should refer Shortcode function from WordPress Codex. Assuming your taxonomy name is category, I made some changes in your current code it should work as per your requirements.
/**
* [children category="5,7,8"]
*/
add_shortcode( 'children' , 'display_custom_post_type' );
function display_custom_post_type($atts) {
$atts = shortcode_atts( array(
'category' => ''
), $atts );
//If category is multiple: 8,9,3
$categories = explode(',' , $atts['category']);
$args = array(
'post_type' => 'children',
'post_status' => 'publish',
'orderby' => 'title',
'order' => 'ASC',
'posts_per_page'=> -1,
'tax_query' => array( array(
'taxonomy' => 'category',
'field' => 'term_id',
'terms' => $categories
) )
);
$string = '';
$query = new WP_Query( $args );
if( ! $query->have_posts() ) {
return false;
}
while( $query->have_posts() ){
$query->the_post();
$string .= '<div id="childWrapper"><div id="childImage">' . get_the_post_thumbnail() . '</div><div style="clear: both;"></div><div id="childName">' . get_the_title() . '</div><div style="clear: both;"></div></div>';
}
wp_reset_postdata();
return $string;
}
In the account area of my woocommerce shopping site you can click 'orders' and see your order i.e. completed, cancelled, on hold etc etc.
However when you click 'collection' (a page I have made within the account area) I want it to display only 'completed' items.
I have tried to put the following code into the functions.php but with no luck:
(ref - https://businessbloomer.com/woocommerce-display-products-purchased-user/)
add_shortcode( 'my_products', 'bbloomer_user_products_bought' );
function bbloomer_user_products_bought() {
global $product, $woocommerce, $woocommerce_loop;
$columns = 3;
$current_user = wp_get_current_user();
$args = array(
'post_type' => 'product',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => '_visibility',
'value' => array('catalog', 'visible'),
'compare' => 'IN'
)
)
);
$loop = new WP_Query($args);
ob_start();
woocommerce_product_loop_start();
Loop part
while ( $loop->have_posts() ) : $loop->the_post();
$theid = get_the_ID();
if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $theid ) ) {
wc_get_template_part( 'content', 'product' );
}
endwhile;
woocommerce_product_loop_end();
woocommerce_reset_loop();
wp_reset_postdata();
return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';
}
and then adding the shortcode:
[my_products]
when you click the collection link it does not display anything - well generates a div with nothing in it!
Is there a better way of doing this or a alternative?
Replace your $args with bellow code
$args = array(
'post_type' => 'shop_order',
'post_status' => 'wc-completed'
);
Then let me know the result. Thanks
My pagination is throwing a 404 error when the posts from General -> Reading are smaller than my custom number of posts on my custom taxonomy cities (custom post type city). From what I saw on SO this issue can be fixed usually by using a filter of pre_get_posts, but don't know how to apply it on my case.
I want to mention that in the taxonomy-cities.php I am getting posts of a category of a custom taxonomy of a custom post type.
taxonomy-cities.php
$cat_ID = get_query_var('cities');
$custom_id = get_term_by('slug', $cat_ID, 'cities');
add_filter('posts_orderby', 'edit_posts_orderby');
function edit_posts_orderby($orderby_statement) {
global $aPostsIDs;
$orderby_statement = 'FIELD(ID, '.implode(',',$_SESSION['saved_city_ids']).')';
return $orderby_statement;
}
$offset = ($paged - 1) * $num_city_guides_post;
$args['post_type'] = 'city';
$args['posts_per_page'] = $num_city_guides_post; // 5
$args['orderby'] = $orderby; // rand
$args['order'] = $order; // DESC
$args['paged'] = $paged; // 1
$args['tax_query'] = array(
array(
'taxonomy' => 'cities',
'field' => 'id',
'terms' => array($custom_id->term_id) // 3742
)
);
}
$wp_query = new WP_Query($args);
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) : $wp_query->the_post();
// some code here
endwhile;
else: echo 'No Posts';
endif;
wp_reset_postdata();
For archive-city.php I am using this filter in the functions.php and it works fine, but it doesn't get applied to the taxonomy-cities.php so I get 404:
function portfolio_posts_per_page_city( $query ) {
if (array_key_exists('post_type', $query->query_vars)) {
if ( $query->query_vars['post_type'] == 'city' )
$num_city_guides_post = get_option('num_city_post');
if( empty( $num_city_guides_post ) )
$num_city_guides_post = 9;
$query->query_vars['posts_per_page'] = $num_city_guides_post;
return $query;
}
}
if ( !is_admin() ) add_filter( 'pre_get_posts', 'portfolio_posts_per_page_city' );
This is an old question however i recently had the same issue. The following Snippet is in fact the contents of my taxonomy-template.php using the standard WP_Query, example here.
How to properly paginate custom post type taxonomy
The Query
Firstly because it's a taxonomy template, the first line declares $term allowing me to access the term array and therefore the taxonomy id, name url etc.
Secondly i'm setting the term id as a variable to be used in the tax_query parameter. This is used to query my custom post type videos and then get the taxonomy (category) for the loop, stored in $term_id.
The Fix
Because i'm looping through the posts and limiting the results, we effectively need to search for our remaining posts while excluding the previous results and results thereafter. What this means is quite simple:
You must make your custom post types searchable in order for pagination to work:
Important:
'exclude_from_search' => false,
Example taxonomy-template.php
<?php $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); ?>
<?php
$term_id = $term->term_id;
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'videos',
'posts_per_page' => 4,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => $term->taxonomy,
'field' => 'term_id',
'terms' => $term_id
)
)
);
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<!-- Loop -->
<?php endwhile; ?>
<?php if ($the_query->max_num_pages > 1) : ?>
<?php endif; ?>
<?php else: ?>
<!-- Nothing Found -->
<?php endif; ?>
Maybe u can solve this without filters.
It`s work for pagination my custom posts types
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$args = array( 'post_type' =>'cities', 'posts_per_page' => 0, 'paged' => $paged );
$query = query_posts( $args );
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
//Do wordpress Loop
<?php endwhile; else: include( get_404_template() ); ?>
<?php endif; wp_reset_postdata(); ?>
I had the same problem with my custom post taxonomies pagination. Older posts page was coming with a 404 page not found. This issue is related to WP taxonomy slug so you have to rewrite rules for your custom post type taxonomies like this below:
function generate_taxonomy_rewrite_rules( $wp_rewrite ) {
$rules = array();
$post_types = get_post_types( array( 'public' => true, '_builtin' => false ), 'objects' );
$taxonomies = get_taxonomies( array( 'public' => true, '_builtin' => false ), 'objects' );
foreach ( $post_types as $post_type ) {
$post_type_name = $post_type->name;
$post_type_slug = $post_type->rewrite['slug'];
foreach ( $taxonomies as $taxonomy ) {
if ( $taxonomy->object_type[0] == $post_type_name ) {
$terms = get_categories( array( 'type' => $post_type_name, 'taxonomy' => $taxonomy->name, 'hide_empty' => 0 ) );
foreach ( $terms as $term ) {
$rules[$post_type_slug . '/' . $term->slug . '/?$'] = 'index.php?' . $term->taxonomy . '=' . $term->slug;
$rules[$post_type_slug . '/' . $term->slug . '/page/?([0-9]{1,})/?$'] = 'index.php?' . $term->taxonomy . '=' . $term->slug . '&paged=' . $wp_rewrite->preg_index( 1 );
}
}
}
}
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_action('generate_rewrite_rules', 'generate_taxonomy_rewrite_rules');
This should work for all custom post type taxonomies and if you want to be more specific then you can try updating these two variables like this:
$post_types = get_post_types( array( 'name' => 'city', 'public' => true, '_builtin' => false ), 'objects' );
$taxonomies = get_taxonomies( array( 'name' => 'cities', 'public' => true, '_builtin' => false ), 'objects' );
Finally, just use your default query loop without passing any arguments. Pages will be generated based on General -> Reading.