0

I've created a custom post type, and I added to it custom Taxonomies, As well as custom meta boxes, and I did an advanced search with several options. But I have a problem because of the duration loop (Duration: is a custom meta field) when activated in the search box, this message appears to me:

   WordPress database error: [Not unique table/alias: 'wp_postmeta']
SELECT DISTINCT wp_posts.* FROM wp_posts INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id ) LEFT JOIN wp_postmeta ON wp_posts.ID = wp_postmeta.post_id WHERE 1=1 AND ( wp_postmeta.meta_key = '_duration_c_a' ) AND wp_posts.post_type = 'courses' AND (wp_posts.post_status = 'publish') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC    
Notice: Undefined variable: durations in C:\xampp\htdocs\wp1\wp-content\plugins\academy4-sections\academy4-sections.php on line 543

Here I did: Register custom query vars

<?php 


/*  Register custom query vars     */
function mhmd_register_query_vars( $vars ) {
    $vars[] = 'courseid';
    $vars[] = 'duration';
    return $vars;
} 
add_filter( 'query_vars', 'mhmd_register_query_vars' );

/* Build a custom query based on several condition */
function mhmd_pre_get_posts( $query ) {
    // check if the user is requesting an admin page 
    // or current query is not the main query
    if ( is_admin() || ! $query->is_main_query() ){
        return;
    }

    // edit the query only when post type is 'courses'
    // if it isn't, return
    if ( !is_post_type_archive( 'courses' ) ){
        return;
    }

    $meta_query = array();


    // get query var values
    // defaults to empty string
    if( !empty( get_query_var( 'duration' ) ) ){
        $meta_query[] = array( 'key' => '_duration_c_a', 'value' => get_query_var( 'duration' ), 'compare' => 'LIKE' );
    }
    if( !empty( get_query_var( 'courseid' ) ) ){
        $meta_query[] = array( 'key' => '_course_id_c_a', 'value' => get_query_var( 'courseid' ), 'compare' => 'LIKE' );
    }
    if( count( $meta_query ) > 1 ){
        $meta_query['relation'] = 'AND';
    }
    if( count( $meta_query ) > 0 ){
        $query->set( 'meta_query', $meta_query );
    }
}
add_action( 'pre_get_posts', 'mhmd_pre_get_posts', 1 );

and here I create meta box loop for select it in search

  /**
  * Build search form markup
  */
 function mhmd_search_form( $args ){

    // The Query
    // meta_query expects nested arrays, even if you only have one query
    // to add the category param

        $sm_query = new WP_Query( array( 'post_type' => 'courses', 'posts_per_page' => '-1', 'meta_query' => array( array( 'key' => '_duration_c_a',                     'operator' => 'NOT EXISTS', 
 ) ) ) );

    // The Loop
    if ( $sm_query->have_posts() ) {
        $durations = array();
        while ( $sm_query->have_posts() ) {
            $sm_query->the_post();
            $duration = get_post_meta( get_the_ID(), '_duration_c_a', true );

            // populate an array of all occurrences (non duplicated)
            if( !in_array( $duration, $durations ) ){
                $durations[] = $duration;    
            }
        }
    }

    /* Restore original Post Data */
    wp_reset_postdata();

    if( count($durations) == 0){
        return;
    }
    asort($durations);

    $select_duration = '<select name="duration">';
    $select_duration .= '<option value="">' . __( 'Select duration', 'smashing_plugin' ) . '</option>';
    foreach ($durations as $duration ) {
        $select_duration .= '<option value="' . $duration . '">' . $duration . '</option>';
    }
    $select_duration .= '</select>' . "\n";

    reset($durations);
  • Your error is fairly specific - in line 543 your $durations variable isn't set. Which line of code above is 543? It looks like you only set $durations if your $sm_query has posts, so I would check first there - add an else for in case there are no posts, set $durations to arbitrary values, see if that resolves the error. If so, the issue is you are not getting any post results in your query. – WebElaine Sep 19 '17 at 13:50
  • Thank you for your reply, dear. The problem is not data redundancy Before searching, the problem starts after the search. – mhmd_bdr Sep 22 '17 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.