Adding custom fields to the search and results - wordpress

I'm using the plugin "WP Search Suggest" to suggest in the search field.
Now, the plugin works good, but it only suggest and search titles.
I looked in the plugin and found this:
$query_args = apply_filters(
'wpss_search_query_args',
array(
's' => $s,
'post_status' => 'publish',
'post_type' => 'movie',
),
$s
);
$query = new WP_Query( $query_args );
if ( $query->posts ) {
foreach ( $query->posts as $post ) {
$results[] = $post->post_title;
}
$results = apply_filters(
'wpss_search_results',
$results,
$query
);
echo join( $results, "\n" );
}
and I tried to add support to custom field by adding:
'meta_key' => 'somthing',
'meta_value' => '$s'
but I didn't work, and didn't found a way to 'echo' the results because the '$query->posts' array don't hold the meta keys or values...

Related

Woocommerce single-product page - Show all variation images

For a special gallery i need to show all woocommerce variation images. On the woocommerce content-single-product.php i can access the variations but i cant get the image url out of it. How can i do that?
Inside my content-single-product.php overwrite:
<?php
$args = array(
'post_type' => 'product_variation',
'post_status' => array( 'private', 'publish' ),
'numberposts' => -1,
'orderby' => 'menu_order',
'order' => 'asc',
'post_parent' => $post->ID
);
$variations = get_posts( $args );
echo "<pre>"; print_r($variations); echo "</pre>";
?>
You can do something like this.
$product = new WC_Product_Variable( $product_id );
// get the product variations
$product_variations = $product->get_available_variations();
if ( !empty( $product_variations ) ) {
foreach($product_variations as $product_variation) {
echo $product_variation['image_src'];
}
}

Wordpress - Query comments by post meta

I am using the code below (simplified) to display a list of the last 10 comments:
<?php
$args = array(
'post_type' => 'tarefa',
'number' => '10',
'order' => 'DESC',
'orderby' => 'comment_date',
//'meta_key' => 'field_name',
//'meta_value' => 'field_value',
);
$comments_query = new WP_Comment_Query;
$comments = $comments_query->query( $args );
foreach ( $comments as $comment ) {
echo '<p>';
echo get_the_title($comment->comment_post_ID) . '<br>'; //post title
echo $comment->comment_content; // comment content
echo '</p>';
};
?>
Question:
Well, meta_key and meta_value seem to be associated to comment_meta... But in my case, I have to display comments based on post_meta key and value.
Any sugestions?
You can try this code.
You need to add a query for posts to get array of post ides with meta key.
Then use that array into comments query argument.
//QUERY FOR POSTS WITH META KEY AND VALUE (META QUERY)
$post_args = array(
'post_type' => 'post',
'meta_key' => 'meta key',//Meta key of post
'meta_value' => 'meta value',//String or Numeric value
'meta_compare' => '=',
);
$post_query = new WP_Query( $post_args );
$posts_array= array();
if ( $post_query->have_posts() ) {
while ( $post_query->have_posts() ) {
$post_query->the_post();
$posts_array[] = get_the_ID(); //Array of post ids
}
wp_reset_postdata();
}
//YOUR COMMENT ARGS SHOULD BE THIS
$args = array(
'post_type' => 'tarefa',
'number' => '10',
'order' => 'DESC',
'orderby' => 'comment_date',
'post__in' => $posts_array, //THIS IS THE ARRAY OF POST IDS WITH META QUERY
);
Try this , then let me know the result.
My first question here at Stackoverflow and that worked perfectly.
Thank you very much, Souvik!
Below the final result (simplified):
$post_args = array(
'post_type' => 'tarefa',
'posts_per_page' => -1,
'meta_key' => 'field_name',
'meta_value' => 'field_value',
);
$post_query = new WP_Query( $post_args );
$posts_array= array();
if ( $post_query->have_posts() ) {
while ( $post_query->have_posts() ) {
$post_query->the_post();
$posts_array[] = get_the_ID(); //Array of post ids
}
wp_reset_postdata();
}
//YOUR COMMENT ARGS SHOULD BE THIS
$args = array(
'number' => '30',
'order' => 'DESC',
'orderby' => 'comment_date',
'post__in' => $posts_array, //THIS IS THE ARRAY OF POST IDS WITH META QUERY
);
$comments_query = new WP_Comment_Query;
$comments = $comments_query->query( $args );
foreach ( $comments as $comment ) {
echo '<p>';
echo get_the_title($comment->comment_post_ID) . '<br>'; //post title
echo $comment->comment_content; // comment content
echo '</p>';
};

WordPress shortcodes for terms

I was trying to create shortcode for custom taxonomy terms dynamically. But failing to do so.
Suppose, if there is a term called "wordpress" then I should be able to query all the posts associated with that term via shortcode.
To be more precise, suppose if there is a taxonomy called 'event' and under that taxonomy there are multiple terms. So, I was trying to query posts under each of the term via shortcode of each of the term.
Here is what I tried:
function wordpress_recent_post( $atts, $content ) {
$a = shortcode_atts( array(
'cat' => '',
), $atts );
$args = array(
'posts_per_page' => 1,
'offset' => 0,
'category_name' => $a['cat'],
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'post',
'post_status' => 'publish',
'ignore_sticky_posts' => true,
);
$recent_posts = new WP_Query( $args );
ob_start();
if ( ! $recent_posts-> have_posts() ) {
return 'No Posts Found for ' . $a['cat'];
}
while ( $recent_posts->have_posts() ) {
$recent_posts->the_post();
the_title( '<h2>', '</h2>' );
if ( '' != $a['cat'] ) {
$href = '/category/' . $a['cat'];
} else {
$href = '/blog';
}
echo "<p><a href='$href'>Read More" . ucwords( $a['cat'] ) . '</a></p>';
}
wp_reset_query();
return ob_get_clean();
}
add_shortcode( 'wordpress_recent_post', array($this, 'wordpress_recent_post') );
And then I used this to call the posts from a term called "features" whose id is '183' (suppose)
[wordpress_recent_post cat="183"]
Any help would be really very appreciable.
Thanks!
Adding term slug did it. There should be slug not id, like this:
[wordpress_recent_post cat="features"]

Need example code for wp_playlist_shortcode

I trying to use wp_playlist_shortcode for creating playlist with audio-files from all blog posts.
In official documentation i saw this parameter:
'ids'
(array) Create a playlist out of these explicit attachment IDs. If empty, a playlist will be created from all $type attachments of $id. Default empty.
I trying this code, and its doesn't working:
$attch_id = array('76', '73', '70', '67');
wp_playlist_shortcode( array( 'ids' => '$attch_id' );
How to create playlist with audio-files from all blog posts? Now i use this code, but it is a not playlist.
$audios = array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'post_status' => null, 'post_parent' => null, 'post_mime_type' => 'audio/mpeg' );
$attachments = get_posts( $audios );
if ($attachments) {
foreach ( $attachments as $post ) {
setup_postdata($post);
$media_url = $post->guid;
$media_title = $post->post_title;
echo wp_audio_shortcode( array( 'src' => $media_url) );
echo '<p>' . $media_title . '</p>';
// print_r($media_url);
}
}
wp_reset_postdata();

add_filter (to posts_where) callback function is not calling

I made a file (test.php) in my WordPress root folder, and load it separately from WordPress like this http://localhost/MyWP_Website/test.php
Here are my code:
<?php
require_once("wp-config.php");
require_once("wp-includes/wp-db.php");
function myFilter001($where = '') {
GLOBAL $wpdb;
$where .= " AND ".$wpdb->posts.".ID > 20 ";
return $where;
}
add_filter('posts_where', 'myFilter001');
$args = array(
'posts_per_page' => $total,
'category' => $category,
'author' => $author,
's' => $search,
'offset' => 0,
'orderby' => 'ID',
'order' => 'DESC',
);
$posts = get_posts( $args );
?>
I searched and find that my function is not public but how to make it public? it's with my main code and in my only file that I need?!
I found the solution!
By using WP_Query() instead of get_posts() all filters added to posts_where will working, I just replaced last line of my code.
Replace this:
$posts = get_posts( $args );
To this:
$query = new WP_Query( $args );

Resources