Support » Fixing WordPress » A problem with the database when searching for the Meta box

  • mohammad albadrany

    (@mohammad-albadrany)


    I created a custom post type, named it “courses”, then added a set of classifications and meta boxes, and then I did advanced and specialized search, so far everything is working normally
    But once I do the research, I get the following message:

       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

    Of course, I learned the way from this topic when adding the original topic there are no problems. Please help me and here my code:

    
    <?php 
    
    /**
     * Register custom query vars
     *
     * @link https://codex.wordpress.org/Plugin_API/Filter_Reference/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 conditions
     * The pre_get_posts action gives developers access to the $query object by reference
     * any changes you make to $query are made directly to the original object - no return value is requested
     *
     * @link https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
     *
     */
    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 );
    /**
     * 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);
    
        $select_month = '<select name="monthnum">';
        $select_month .= '<option value="">' . __( 'Select month', 'smashing_plugin' ) . '</option>';
        foreach( range(1,12) as $month_of_year ) {
            $select_month .= '<option value="' . $month_of_year . '">' . $month_of_year . '</option>';
        }
        $select_month .= '</select>' . "\n";
    	
        $select_year = '<select name="year">';
        $select_year .= '<option value="">' . __( 'Select year', 'smashing_plugin' ) . '</option>';
        foreach( range(2000,date('Y')) as $_year ) {
            $select_year .= '<option value="' . $_year . '">' . $_year . '</option>';
        }
        $select_year .= '</select>' . "\n";
    
        
        $args = array( 'hide_empty' => false );
        $category_courses_terms = get_terms( 'category-courses', $args );
        if( is_array( $category_courses_terms ) ){
        	$select_category_courses = '<select name="category-courses">';
        	$select_category_courses .= '<option value="" selected="selected">' . __( 'Select typology', 'smashing_plugin' ) . '</option>';
        	foreach ( $category_courses_terms as $term ) {
        		$select_category_courses .= '<option value="' . $term->slug . '">' . $term->name . '</option>';
        	}
        	$select_category_courses .= '</select>' . "\n";
        }
    	
        $args = array( 'hide_empty' => false );
        $venue_courses_terms = get_terms( 'venue-courses', $args );
        if( is_array( $venue_courses_terms ) ){
        	$select_venue_courses = '<select name="venue-courses">';
        	$select_venue_courses .= '<option value="" selected="selected">' . __( 'Select venue', 'smashing_plugin' ) . '</option>';
        	foreach ( $venue_courses_terms as $term ) {
        		$select_venue_courses .= '<option value="' . $term->slug . '">' . $term->name . '</option>';
        	}
        	$select_venue_courses .= '</select>' . "\n";
        }
    	
        $args = array( 'hide_empty' => false );
        $program_courses_terms = get_terms( 'program-courses', $args );
        if( is_array( $program_courses_terms ) ){
        	$select_program_courses = '<select name="program-courses">';
        	$select_program_courses .= '<option value="" selected="selected">' . __( 'Select program', 'smashing_plugin' ) . '</option>';
        	foreach ( $program_courses_terms as $term ) {
        		$select_program_courses .= '<option value="' . $term->slug . '">' . $term->name . '</option>';
        	}
        	$select_program_courses .= '</select>' . "\n";
        }
    
        $output = '<div class="container" id="search-course-style">';
    	$output .= '<div class="col-xs-12"> <h1>search course</h1>';
    	
        $output .= '<form id="smform" action="' . esc_url( home_url() ) . '" method="GET" role="search">';
    	$output .= '<div class="col-xs-12">' . '<input type="text" name="s" placeholder="courseid" value="' . get_search_query() . '" /></div>';
        $output .= '<div class="col-sm-4">' . $select_month . '</div>';
        $output .= '<div class="col-sm-4">' . $select_year . '</div>';
        $output .= '<div class="col-sm-4">' . $select_duration . '</div>';
        $output .= '</div>';
    	$output .= '<div class="col-xs-12">';
        $output .= '<div class="col-sm-4">' . $select_category_courses . '</div>';
        $output .= '<div class="col-sm-4">' . $select_venue_courses . '</div>';
        $output .= '<div class="col-sm-4">' . $select_program_courses . '</div>';
    	$output .= '</div>';
    	$output .= '<div class="col-xs-12">';
        $output .= '<input type="hidden" name="post_type" value="courses" />';
        $output .= '<div class="col-xs-4"><input type="submit" value="Go!" class="bot-slideup-search" /></div></form>';
    	$output .= '</div>';
    	$output .= '</div>';
        return $output;
    }
    
Viewing 2 replies - 1 through 2 (of 2 total)
  • Howdy_McGee

    (@howdy_mcgee)

    Your $durations variable inside your IF conditional of the loop is only instantiated IF there’s things to loop through. IF there isn’t it’s undefinied and thus unavailable whenever you test for if( count($durations) == 0){.

    Define $durations = array() outside the conditional, toward the top so it’s available throughout your entire function call.

    mohammad albadrany

    (@mohammad-albadrany)

    Thank you, my dear brother,
    If you meant to put the code as follows.

    
    	    $sm_query = new WP_Query( array( 'post_type' => 'courses', 'posts_per_page' => '-1', 'meta_query' => array( array( 'key' => '_duration_c_a' 
     ) ) ) );
            $durations = array();
    
        // The Loop
        if ( $sm_query->have_posts() ) {
            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;    
                }
            }
        }
    

    Actually, I did it but the method did not work.
    If the method is wrong, please put the correct method here
    I am very thankful you

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘A problem with the database when searching for the Meta box’ is closed to new replies.