Avoid looking into Post content while searching - wordpress

I have created a query using WP_Query for searching posts. Below is the query
$args = array(
's' => 'Keyword'
);
$the_query = new WP_Query( $args );
But the argument 's' always looks into post_title and post_content. I want to change it so it look into post_title only and not into post_content. How to do that?

At first you need to modify the search function of wp, using posts_search filter hook Check this Link, then run your query and argument.
After that, you need to remove that hook after use.
The example code is bellow
<?php
function wpse_11826_search_by_title( $search, $wp_query ) {
if ( ! empty( $search ) && ! empty( $wp_query->query_vars['search_terms'] ) ) {
global $wpdb;
$q = $wp_query->query_vars;
$n = ! empty( $q['exact'] ) ? '' : '%';
$search = array();
foreach ( ( array ) $q['search_terms'] as $term )
$search[] = $wpdb->prepare( "$wpdb->posts.post_title LIKE %s", $n . $wpdb->esc_like( $term ) . $n );
if ( ! is_user_logged_in() )
$search[] = "$wpdb->posts.post_password = ''";
$search = ' AND ' . implode( ' AND ', $search );
}
return $search;
}
add_filter( 'posts_search', 'wpse_11826_search_by_title', 10, 2 );
$args = array(
's' => 'Keyword'
);
$the_query = new WP_Query( $args );
remove_filter( 'posts_search', 'wpse_11826_search_by_title', 500 );
//Then your code goes here....
?>

Related

How to show recently viewed products in WooCommerce WordPress version 5.0.3

can you please tell me the coding to include recently viewed products in WooCommerce
Try adding below snippet where you want to display
<div>
<?php
echo do_shortcode("[woocommerce_recently_viewed_products per_page='5']");
?>
</div>
Add below code to functions.php of your active theme
function rc_woocommerce_recently_viewed_products( $atts, $content = null ) {
// Get shortcode parameters
extract(shortcode_atts(array(
"per_page" => '5'
), $atts));
// Get WooCommerce Global
global $woocommerce;
// Get recently viewed product cookies data
$viewed_products = ! empty( $_COOKIE['woocommerce_recently_viewed'] ) ? (array) explode( '|', $_COOKIE['woocommerce_recently_viewed'] ) : array();
$viewed_products = array_filter( array_map( 'absint', $viewed_products ) );
// If no data, quit
if ( empty( $viewed_products ) )
return __( 'You have not viewed any product yet!', 'rc_wc_rvp' );
// Create the object
ob_start();
// Get products per page
if( !isset( $per_page ) ? $number = 5 : $number = $per_page )
// Create query arguments array
$query_args = array(
'posts_per_page' => $number,
'no_found_rows' => 1,
'post_status' => 'publish',
'post_type' => 'product',
'post__in' => $viewed_products,
'orderby' => 'rand'
);
// Add meta_query to query args
$query_args['meta_query'] = array();
// Check products stock status
$query_args['meta_query'][] = $woocommerce->query->stock_status_meta_query();
// Create a new query
$r = new WP_Query($query_args);
// If query return results
if ( $r->have_posts() ) {
$content = '<ul class="rc_wc_rvp_product_list_widget">';
// Start the loop
while ( $r->have_posts()) {
$r->the_post();
global $product;
$content .= '<li>
<a href="' . get_permalink() . '">
' . ( has_post_thumbnail() ? get_the_post_thumbnail( $r->post->ID, 'shop_thumbnail' ) : woocommerce_placeholder_img( 'shop_thumbnail' ) ) . ' ' . get_the_title() . '
</a> ' . $product->get_price_html() . '
</li>';
}
$content .= '</ul>';
}
// Get clean object
$content .= ob_get_clean();
// Return whole content
return $content;
}
// Register the shortcode
add_shortcode("woocommerce_recently_viewed_products", "rc_woocommerce_recently_viewed_products");

WooCommerce Show price next to product variation

I have a list of variations of a product but I would like to show the price next to it. This way the user can immediately see the price difference between each variations. http://cl.ly/15150P3D1U3k
Any way how to do this in WooCommerce knowing my site is multi-langual?
Thank you!
Please try this. Which i have just tested in the latest WordPress and WooCommerce. Just add it to your functions file.
add_filter( 'woocommerce_variation_option_name', 'display_price_in_variation_option_name' );
function display_price_in_variation_option_name( $term ) {
global $wpdb, $product;
if ( empty( $term ) ) return $term;
if ( empty( $product->id ) ) return $term;
$result = $wpdb->get_col( "SELECT slug FROM {$wpdb->prefix}terms WHERE name = '$term'" );
$term_slug = ( !empty( $result ) ) ? $result[0] : $term;
$query = "SELECT postmeta.post_id AS product_id
FROM {$wpdb->prefix}postmeta AS postmeta
LEFT JOIN {$wpdb->prefix}posts AS products ON ( products.ID = postmeta.post_id )
WHERE postmeta.meta_key LIKE 'attribute_%'
AND postmeta.meta_value = '$term_slug'
AND products.post_parent = $product->id";
$variation_id = $wpdb->get_col( $query );
$parent = wp_get_post_parent_id( $variation_id[0] );
if ( $parent > 0 ) {
$_product = new WC_Product_Variation( $variation_id[0] );
return $term . ' (' . wp_kses( woocommerce_price( $_product->get_price() ), array() ) . ')';
}
return $term;
}

Next / Previous Post Links from Same Category and Alphabetically ordered

RESOLVED
I'm exhaustively searching for a method to provide Next and Previous Post Links in a different way from which it usually appears in Single Post.
By DEFAULT it:
Is chronological ordered
Links to posts from all blog categories
But I NEED it:
ALPHABETICALLY ordered
Linking to posts from SAME CATEGORY only
I'm not a developer but I found two codes and I think if I could merge both the problem would be solved. Could someone help me please?
CODE 1 - Turn Next/Prev links alphabetcally, but not from same category (source)
function filter_next_post_sort($sort) {
$sort = "ORDER BY p.post_title ASC LIMIT 1";
return $sort;
}
function filter_next_post_where($where) {
global $post, $wpdb;
return $wpdb->prepare("WHERE p.post_title > '%s' AND p.post_type = '". get_post_type($post)."' AND p.post_status = 'publish'",$post->post_title);
}
function filter_previous_post_sort($sort) {
$sort = "ORDER BY p.post_title DESC LIMIT 1";
return $sort;
}
function filter_previous_post_where($where) {
global $post, $wpdb;
return $wpdb->prepare("WHERE p.post_title < '%s' AND p.post_type = '". get_post_type($post)."' AND p.post_status = 'publish'",$post->post_title);
}
add_filter('get_next_post_sort', 'filter_next_post_sort');
add_filter('get_next_post_where', 'filter_next_post_where');
add_filter('get_previous_post_sort', 'filter_previous_post_sort');
add_filter('get_previous_post_where', 'filter_previous_post_where');
CODE 2 - Turn Next/Prev links from same category, but not alphabetically (source)
add_filter( 'get_next_post_join', 'navigate_in_same_taxonomy_join', 20);
add_filter( 'get_previous_post_join', 'navigate_in_same_taxonomy_join', 20 );
function navigate_in_same_taxonomy_join() {
global $wpdb;
return " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id";
}
add_filter( 'get_next_post_where' , 'navigate_in_same_taxonomy_where' );
add_filter( 'get_previous_post_where' , 'navigate_in_same_taxonomy_where' );
function navigate_in_same_taxonomy_where( $original ) {
global $wpdb, $post;
$where = '';
$taxonomy = 'category';
$op = ('get_previous_post_where' == current_filter()) ? '<' : '>';
$where = $wpdb->prepare( "AND tt.taxonomy = %s", $taxonomy );
if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) )
return $original ;
$term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
$term_array = array_map( 'intval', $term_array );
if ( ! $term_array || is_wp_error( $term_array ) )
return $original ;
$where = " AND tt.term_id IN (" . implode( ',', $term_array ) . ")";
return $wpdb->prepare( "WHERE p.post_date $op %s AND p.post_type = %s AND p.post_status = 'publish' $where", $post->post_date, $post->post_type );
}
After weeks of searching for a solution, here is the FINAL ANSWER!
THANK YOU FOR HELP ME!
You should use get_adjacent_post(); this is provide you next or previous post.
This is for previous post:
<?php
$prev_post = get_adjacent_post( true, '', true, 'taxonomy_slug' ); ?>
if ( is_a( $prev_post, 'WP_Post' ) ) {
?>
<?php echo get_the_title( $prev_post->ID ); ?>
<?php } ?>
This is for next post:
<?php
$next_post = get_adjacent_post( true, '', false, 'taxonomy_slug' );
if ( is_a( $next_post, 'WP_Post' ) ) {
?>
<?php echo get_the_title( $next_post->ID ); ?>
<?php } ?>

Get images from post's gallery WordPress 3.5

How to get images from a gallery in a post in WordPress 3.5 as gallery is no longer related to posts in 3.5. get_children() doesnot work as gallery is not attachment. Any help is appreciated.
You must probably parse shortcode:
http://codex.wordpress.org/Gallery_Shortcode
Use regular expression:
$post_content = get_the_content();
preg_match('/\[gallery.*ids=.(.*).\]/', $post_content, $ids);
$array_id = explode(",", $ids[1]);
`global $post;
$post_subtitrare = get_post( $post->ID );
$content = $post_subtitrare->post_content;
$pattern = get_shortcode_regex();
preg_match( "/$pattern/s", $content, $match );
if( isset( $match[2] ) && ( "gallery" == $match[2] ) ) {
$atts = shortcode_parse_atts( $match[3] );
$attachments = isset( $atts['ids'] ) ? explode( ',', $atts['ids'] ) : get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . $post->ID .'&order=ASC&orderby=menu_order ID' );
}`
The $attachments will get you what you are used to getting prior to WordPress 3.5.

Exclude Terms from custom Taxonomy?

I have a custom post type in which I have custom taxonomies setup.
I want to print out the categories(custom taxonomy) that a post is included in, but exclude one. I cannot find a solution to exclude the category though.
Here is my code to output a list of the categories the custom post type is filed under:
<?php the_terms( $post->ID, 'critter_cat', 'Critter Type: ', ', ', ' ' ); ?>
How do I exclude a specific category?
Thanks.
You could create a function in your functions.php file that calls get_the_terms to return the list of terms as an array and then remove the item you don't want.
Give this a try:
function get_excluded_terms( $id = 0, $taxonomy, $before = '', $sep = '', $after = '', $exclude = array() ) {
$terms = get_the_terms( $id, $taxonomy );
if ( is_wp_error( $terms ) )
return $terms;
if ( empty( $terms ) )
return false;
foreach ( $terms as $term ) {
if(!in_array($term->term_id,$exclude)) {
$link = get_term_link( $term, $taxonomy );
if ( is_wp_error( $link ) )
return $link;
$excluded_terms[] = '' . $term->name . '';
}
}
$excluded_terms = apply_filters( "term_links-$taxonomy", $excluded_terms );
return $before . join( $sep, $excluded_terms ) . $after;
}
and then use it like this:
<?php echo get_excluded_terms( $post->ID, 'critter_cat', 'Critter Type: ', ', ', ' ', array(667)); ?>

Resources