im new posting in the forum, but here i found answers without ask, just searching, thanks about that.
Here go my questions:
I have a custom post type called 'publicacion' and there the next custom WP_Query:
$query_args = array(
's' => mysql_real_escape_string( $_GET['s'] ),
'post_type' => 'publicacion',
'meta_key' => 'meta-box-fp'
);
if( $_GET['autor'] != '' ) {
$query_args['tax_query'] = array(
array(
'taxonomy' => 'autores',
'field' => 'slug',
'terms' => mysql_real_escape_string( $_GET['autor'] )
)
);
}
if( trim( $_GET['fini'] ) != '' || trim( $_GET['ffin'] ) != '' ) {
$query_args['meta_query'] = array (
array(
'key' => 'meta-box-fp',
'value' => array( mysql_real_escape_string( $_GET['fini'] ) . '-00-00', mysql_real_escape_string( $_GET['ffin'] ) . '-00-00' ),
'type' => 'DATE',
'compare' => 'BETWEEN' )
);
}
if( isset( $_GET['ord_by'] ) ) {
$ordby = mysql_real_escape_string( $_GET['ord_by'] ) == 'year' ? 'meta_value' : mysql_real_escape_string( $_GET['ord_by'] );
$query_args['orderby'] = $ordby;//mysql_escape_string( $_GET['ord_by'] );
$query_args['order'] = 'DESC';//mysql_escape_string( $_GET['ord'] );
}
$query = new WP_Query( $query_args );
The 's' parameter redirects to the search page, but i need do the search in the archive-publicacion.php file, and i need the 's' parameter, search for excerpt instead content.
It is possible to do that? Thanks and i hope be clear in my question and excuse me my english.
well.. i've resolved my issue by adding the query parameters in functions.php from theme instead from archive.php and changing the 's' to 'str' from the url for not redirect the query when wordpress reads the url.
This looks something like this functions.php:
add_action( 'pre_get_posts', 'my_custom_search' );
function my_custom_search( $query ) {
if( !is_admin() && $query->is_main_query() && $query->get('post_type') == 'publicacion' ) {
if( isset( $_GET['str'] ) && !empty( $_GET['str'] ) ) {
$query->set( 's', mysql_real_escape_string( $_GET['str'] ) );
}
if( ( isset( $_GET['fini'] ) && !empty( $_GET['fini'] ) ) && ( isset( $_GET['ffin'] ) && !empty( $_GET['ffin'] ) ) ) {
$fini = !empty( $_GET['fini'] ) ? mysql_real_escape_string( $_GET['fini'] ) . '-00-00' : '1900-00-00';
$ffin = !empty( $_GET['ffin'] ) ? mysql_real_escape_string( $_GET['ffin'] ) . '-00-00' : date( 'Y' ) . '-00-00';
$args_meta_query = array(
array(
'key' => 'meta-box-fp',
'value' => array( $fini, $ffin ),
'type' => 'DATE',
'compare' => 'BETWEEN'
)
);
$query->set( 'meta_query', $args_meta_query );
}
if( isset( $_GET['autor'] ) && !empty( $_GET['autor'] ) ) {
$args_tax_query = array(
array(
'taxonomy' => 'autores',
'field' => 'slug',
'terms' => mysql_real_escape_string( $_GET['autor'] )
)
);
$query->set( 'tax_query', $args_tax_query );
}
if( isset( $_GET['ordby'] ) && $_GET['ordby'] != 'fecha' ) {
$ordby = mysql_real_escape_string( $_GET['ordby'] );
$query->set( 'orderby', $ordby );
$query->set( 'order', strtoupper( mysql_real_escape_string( $_GET['ord'] ) ) );
}else {
$ord = isset( $_GET['ord'] ) && !empty( $_GET['ord'] ) ? strtoupper( mysql_real_escape_string( $_GET['ord'] ) ) : 'DESC';
$query->set( 'orderby', 'meta_value_num' );
$query->set( 'meta_key', 'meta-box-fp' );
$query->set( 'order', $ord );
}
}
}
I hope this help somebody and excuse me my english again.
Bye.
Related
I am using "Sensei LMS" and "Sensei LMS Certificates" plugins.
Now , to enhance "Sensei LMS Certificates" plugin functionality , I have created a custom meta box and custom fields like this : https://prnt.sc/104s7oy
I am using this action hook sensei_certificates_before_pdf_output to show text in PDF
I have added the following code in my custom plugin :
<?php
add_action( 'sensei_certificates_before_pdf_output', 'action__sensei_certificates_before_pdf_output' , 20, 2 );
function action__sensei_certificates_before_pdf_output( $pdf_certificate, $fpdf ) {
global $woothemes_sensei_certificates;
$show_border = apply_filters( 'woothemes_sensei_certificates_show_border', 0 );
$start_position = 200;
$args = array(
'post_type' => 'certificate',
'meta_key' => 'certificate_hash',
'meta_value' => $pdf_certificate->hash,
);
$query = new WP_Query( $args );
$certificate_id = 0;
if ( $query->have_posts() ) {
$query->the_post();
$certificate_id = $query->posts[0]->ID;
}
wp_reset_query();
if ( 0 < intval( $certificate_id ) ) {
$user_id = get_post_meta( $certificate_id, 'learner_id', true );
$student = get_userdata( $user_id );
$student_name = $student->display_name;
$fname = $student->first_name;
$lname = $student->last_name;
if ( '' != $fname && '' != $lname ) {
$student_name = $fname . ' ' . $lname;
}
$course_id = get_post_meta( $certificate_id, 'course_id', true );
$course_title = get_post_field('post_title', $course_id);
$course_end = Sensei_Utils::sensei_check_for_activity(
array(
'post_id' => intval( $course_id ),
'user_id' => intval( $user_id ),
'type' => 'sensei_course_status',
),
true
);
$course_end_date = $course_end->comment_date;
$date = Woothemes_Sensei_Certificates_Utils::get_certificate_formatted_date( $course_end_date );
if ( isset( $woothemes_sensei_certificates->certificate_template_fields['certificate_custom_message']['text'] ) && '' != $woothemes_sensei_certificates->certificate_template_fields['certificate_custom_message']['text'] ) {
$certificate_custom_message = $woothemes_sensei_certificates->certificate_template_fields['certificate_custom_message']['text'];
$certificate_custom_message .= str_replace( array( '{{learner}}', '{{course_title}}', '{{completion_date}}', '{{course_place}}' ), array( $student_name, $course_title, $date, get_bloginfo( 'name' ) ), $certificate_custom_message );
}
$output_fields = array(
'certificate_custom_message' => 'textarea_field',
);
foreach ( $output_fields as $meta_key => $function_name ) {
if ( isset( $woothemes_sensei_certificates->certificate_template_fields[ $meta_key ]['position']['x1'] ) ) {
$font_settings = $woothemes_sensei_certificates->get_certificate_font_settings( $meta_key );
call_user_func_array( array( $pdf_certificate, $function_name ), array( $fpdf, $meta_key, $show_border, array( $woothemes_sensei_certificates->certificate_template_fields[ $meta_key ]['position']['x1'], $woothemes_sensei_certificates->certificate_template_fields[ $meta_key ]['position']['y1'], $woothemes_sensei_certificates->certificate_template_fields[ $meta_key ]['position']['width'], $woothemes_sensei_certificates->certificate_template_fields[ $meta_key ]['position']['height'] ), $font_settings ) );
}
}
} else {
wp_die( esc_html__( 'The certificate you are searching for does not exist.', '' ), esc_html__( 'Certificate Error', '' ) );
}
}
but this is how text showing inside PDF : https://prnt.sc/104sg3v
expected result is "Hello Test hii" instead of "certificate_custom_message"
How do I fix this ?
I've been working with this function and for some reason, I cannot seem to figure out the issue here.
I have a function that I want to affect the search results to change the number of posts_per_page of the query to be unlimited.
For whatever reason, $query->set('posts_per_page', -1) and $query->set('post__in', $post__in) appear to do nothing while $query->set('tax_query', $tax_query) does work.
Any ideas would be much appreciated!
function fp_search_query_mod( $query ) {
if ( $query->is_search && $query->is_main_query() && !is_admin() ) :
$cats = ( isset($_GET['cats'] ) ? $_GET['cats'] : null );
$post__in = array();
if ( !empty( $cats ) ) :
$cats = explode( ',', $cats );
$tax_query = array(
'relation' => 'AND',
array(
'taxonomy' => 'product_type',
'field' => 'slug',
'terms' => array( 'simple', 'variable' )
),
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $cats,
)
);
$query->set( 'tax_query', $tax_query );
if ( in_array( 'favs', $cats ) ) :
$fp_favs = get_user_meta( get_current_user_id(), 'fp_favs', true );
if ( !empty( $fp_favs ) ) {
$post__in = $fp_favs;
}
endif;
if ( in_array( 'purchases', $cats ) ) :
// $purchased = get_customer_purchased_products();
$purchased = get_user_meta( get_current_user_id(), 'fp_purchases', true );
if ( !empty( $post__in ) ) {
$post__in = array_merge( $post__in, $purchased );
$post__in = array_unique( $post__in );
} else {
$post__in = $purchased;
}
endif;
endif;
if ( !empty( $post__in ) ) :
$query->set( 'post__in', $post__in );
endif;
$query->set( 'posts_per_page', -1 );
endif;
}
add_action( 'pre_get_posts', 'fp_search_query_mod', 2 );
Here's the page on which the resuls are displayed: https://filmpacdev.wpengine.com/?s=beaches&post_type=product
First time I post here ! Hope my question will be relevant.
I defined a function to exclude a category in a calendar which works well when I execute it like this:
function tribe_exclude_events_category_month_list( $query ) {
if ( isset( $query->query_vars['eventDisplay'] ) && ! is_singular( 'tribe_events' ) ) {
if ( $query->query_vars['eventDisplay'] == 'list' && ! is_tax( Tribe__Events__Main::TAXONOMY ) || $query->query_vars['eventDisplay'] == 'month' && $query->query_vars['post_type'] == Tribe__Events__Main::POSTTYPE && ! is_tax( Tribe__Events__Main::TAXONOMY ) && empty( $query->query_vars['suppress_filters'] ) ) {
$query->set( 'tax_query', array(
array(
'taxonomy' => Tribe__Events__Main::TAXONOMY,
'field' => 'slug',
'terms' => array( 'reunions'),
'operator' => 'NOT IN'
)
) );
}
}
return $query;} add_action( 'pre_get_posts', 'tribe_exclude_events_category_month_list' );
But what I want is to execute it if only if user is logged in.
So I did this :
function tribe_exclude_events_category_month_list( $query ) {
if ( isset( $query->query_vars['eventDisplay'] ) && ! is_singular( 'tribe_events' ) ) {
if ( $query->query_vars['eventDisplay'] == 'list' && ! is_tax( Tribe__Events__Main::TAXONOMY ) || $query->query_vars['eventDisplay'] == 'month' && $query->query_vars['post_type'] == Tribe__Events__Main::POSTTYPE && ! is_tax( Tribe__Events__Main::TAXONOMY ) && empty( $query->query_vars['suppress_filters'] ) ) {
$query->set( 'tax_query', array(
array(
'taxonomy' => Tribe__Events__Main::TAXONOMY,
'field' => 'slug',
'terms' => array( 'reunions'),
'operator' => 'NOT IN'
)
) );
}
}
return $query; }
function exclude_category_when_logged_in() {
if ( is_user_logged_in() ) {
add_action( 'pre_get_posts', 'tribe_exclude_events_category_month_list' );
}} add_action( 'init', 'exclude_category_when_logged_in' );
But this doesn't seem to work.
Could you help please ? Thanks a lot Lucas
Try this something like this
if (is_user_logged_in()){
}else{
}
Here is your full code
if (is_user_logged_in()){
function tribe_exclude_events_category_month_list( $query ) {
if ( isset( $query->query_vars['eventDisplay'] ) && ! is_singular( 'tribe_events' ) ) {
if ( $query->query_vars['eventDisplay'] == 'list' && ! is_tax( Tribe__Events__Main::TAXONOMY ) || $query->query_vars['eventDisplay'] == 'month' && $query->query_vars['post_type'] == Tribe__Events__Main::POSTTYPE && ! is_tax( Tribe__Events__Main::TAXONOMY ) && empty( $query->query_vars['suppress_filters'] ) ) {
$query->set( 'tax_query', array(
array(
'taxonomy' => Tribe__Events__Main::TAXONOMY,
'field' => 'slug',
'terms' => array( 'reunions'),
'operator' => 'NOT IN'
)
) );
}
}
return $query;
}
add_action( 'pre_get_posts', 'tribe_exclude_events_category_month_list' );
}else{
//leave empty
}
I have in the Wordpress functions.php the next code that changes the main_query when posts are sorted using a form. The question is: how to exclude from the main_query the second array of the meta_query when the $country variable is empty?
if ( $query->is_main_query() && ( $orderby || $order || $country ) ) {
if ( in_array( $orderby, array( 'event_start_date' ) ) ) {
$query->set( 'orderby', 'meta_value' );
$query->set( 'order', $order );
$query->set( 'meta_query', array( // WordPress has all the results, now, return only the events after today's date
array(
'key' => 'event_start_date', // Check the start date field
'value' => date_i18n("Y-m-d"), // Set today's date (note the similar format)
'compare' => '>=', // Return the ones greater than or equal to today's date
'type' => 'DATE' // Let WordPress know we're working with date
),
array(
'key' => 'venue_country',
'value' => $country,
)
) );
}
}
UPDATE
A suggestion how to solve this I found here.
The problem was solved in this way:
if ( $query->is_main_query() && ( $orderby || $order || $country ) ) {
if ( in_array( $orderby, array( 'event_start_date' ) ) ) {
$query->set( 'orderby', 'meta_value' );
$query->set( 'order', $order );
if( !empty( $_GET['country'] ) ) {
$query->set( 'meta_query', $metaquery1 );
} else {
$query->set( 'meta_query', $metaquery2 );
}
}
}
I don't know PHP, but I have to work in it. I need add to $attr['ids'] array with $gallery_setting.
function the_featured_image_gallery( $atts = array() ) {
$gallery_setting = get_theme_mod( 'featured_image_gallery' );
if ( is_array( $gallery_setting ) && ! empty( $gallery_setting ) ) {
$atts['ids'] = implode( ',', $gallery_setting );
echo gallery_shortcode( $atts );
}
}
Ideally, I would like:
echo gallery_shortcode( $atts, array(
'order' => 'ASC',
'size' => 'full',
'link' => 'none'
) );
but I know that it does not work.
Please clarify your question. It's not clear what your problem is...
To try a shot in the dark:
function the_featured_image_gallery( $atts = array() ) {
$gallery_setting = get_theme_mod( 'featured_image_gallery' );
if ( is_array( $gallery_setting ) && ! empty( $gallery_setting ) ) {
$atts['ids'] = implode( ',', $gallery_setting );
$additional_atts = array(
'order' => 'ASC',
'size' => 'full',
'link' => 'none'
);
$atts = array_merge($atts, $additional_atts);
echo gallery_shortcode( $atts );
}
}