Forum Replies Created

Viewing 10 replies - 1 through 10 (of 10 total)
  • Thread Starter Bobozar

    (@skizzy)

    I inserted the below query code in the functions.php function

    global $wp_query;
    echo $wp_query->request;

    And the SQL output goes like this.

    SELECT wpxx_posts.* FROM wpxx_posts WHERE 1=1 AND wpxx_posts.ID = 2647 AND wpxx_posts.post_type = 'page' ORDER BY wpxx_posts.post_date DESC SELECT wpxx_posts.* FROM wpxx_posts WHERE 1=1 AND wpxx_posts.ID = 2647 AND wpxx_posts.post_type = 'page' ORDER BY wpxx_posts.post_date DESC SELECT wpxx_posts.* FROM wpxx_posts WHERE 1=1 AND wpxx_posts.ID = 2647 AND wpxx_posts.post_type = 'page' ORDER BY wpxx_posts.post_date DESC SELECT wpxx_posts.* FROM wpxx_posts WHERE 1=1 AND wpxx_posts.ID = 2647 AND wpxx_posts.post_type = 'page' ORDER BY wpxx_posts.post_date DESC SELECT wpxx_posts.* FROM wpxx_posts WHERE 1=1 AND wpxx_posts.ID = 2647 AND wpxx_posts.post_type = 'page' ORDER BY wpxx_posts.post_date DESC SELECT wpxx_posts.* FROM wpxx_posts WHERE 1=1 AND wpxx_posts.ID = 2647 AND wpxx_posts.post_type = 'page' ORDER BY wpxx_posts.post_date DESC

    The function is:

    function my_pre_get_posts( $query ) {
    	
    	// do not modify queries in the admin
    	if( is_admin() ) {
    		
    		return $query;
    		
    	}
    	
    	
    	// only modify queries for 'event' post type
    	//if( isset($query->query_vars['post_type'] && $query->query_vars['post_type'] == 'post')) {#
    	if( isset( $query->query_vars['post_type']) && $query->query_vars['post_type'] == 'post') {
    	
    		// allow the url to alter the query
    		if( isset($_GET['country']) ) {
    			
        		$query->set('meta_key', 'country');
    			$query->set('meta_value', $_GET['country']);
    			
        	} 
    			
        	
    	}
    	
    	global $wp_query;
        echo $wp_query->request;
    	// return
    	return $query;
    
    }
    
    add_action('pre_get_posts', 'my_pre_get_posts');
    • This reply was modified 8 years, 9 months ago by Bobozar.
    Thread Starter Bobozar

    (@skizzy)

    When I adjusted the code and inserted dnyanraja’s meta_query alternative, I got the below error:

    Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW) in C:\wamp64\www\\wp-content\themes\look\functions.php on line 20

    Line 20 reads:

    
    $query->set('meta_query' => array(

    Main code:

    function my_pre_get_posts( $query ) {
    	
    	// do not modify queries in the admin
    	if( is_admin() ) {
    		
    		return $query;
    		
    	}
    	
    	
    	// only modify queries for 'event' post type
    	//if( isset($query->query_vars['post_type'] && $query->query_vars['post_type'] == 'post')) {#
    	if( isset( $query->query_vars['post_type']) && $query->query_vars['post_type'] == 'post') {
    	
    		// allow the url to alter the query
    		if( isset($_GET['country']) ) {
    			
        		$query->set('meta_query' => array(
                   'key' => 'country',
                   'value' => $_GET[‘country’],
                   'compare' => 'LIKE'
                )
             )
    			
        	} 
    	}
    	
    	
    	// return
    	return $query;
    
    }
    
    add_action('pre_get_posts', 'my_pre_get_posts');

    But when I changed it to the below code

    function my_pre_get_posts( $query ) {
    	
    	// do not modify queries in the admin
    	if( is_admin() ) {
    		
    		return $query;
    		
    	}
    	
    	
    	// only modify queries for 'event' post type
    	//if( isset($query->query_vars['post_type'] && $query->query_vars['post_type'] == 'post')) {#
    	if( isset( $query->query_vars['post_type']) && $query->query_vars['post_type'] == 'post') {
    	
    		// allow the url to alter the query
    		if( isset($_GET['country']) ) {
    			
        		$query->set('meta_key', 'country');
    			$query->set('meta_value', $_GET['country']);
    			
        	} 
    			
        	
    	}
    	
    	
    	// return
    	return $query;
    
    }
    
    add_action('pre_get_posts', 'my_pre_get_posts');

    And I visited the url example.com/?country=us

    I got ‘Sorry, Posts you requested could not be found…’

    With this I guess it is not filtering the field.

    Thread Starter Bobozar

    (@skizzy)

    After trying dnyanraja’s answer, I got the error below:

    Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead) in C:\wamp64\www\\wp-content\themes\look\functions.php on line 14

    On line 14, I have the below code:

    
    if( isset($query->query_vars['post_type'] && $query->query_vars['post_type'] == 'post')) {
    Thread Starter Bobozar

    (@skizzy)

    Thanks for your input. Can you help with a code solution. I’m not a PHP developer so I don’t know how to go about writing the code.

    Bobozar

    (@skizzy)

    Have you solved the problem?

    Thread Starter Bobozar

    (@skizzy)

    It would be nice if those features can be added on time. Thanks so much!!

    Thread Starter Bobozar

    (@skizzy)

    Hello, I followed your steps. Even before that, I deactivated Jetpack and activate it back again. Yet no luck.

    I deleted Jetpack through FTP and download and put it back manually and connect to wordpress with another account, yet no luck!

    Don’t know what to do again.

    Thread Starter Bobozar

    (@skizzy)

    F12 is not poping up anything, so I can’t check any lines. I’ve sent you a mail. Kindly check it out.

    Thread Starter Bobozar

    (@skizzy)

    Yeah. I’m suing Smart mag theme http://themeforest.net/item/smartmag-responsive-retina-wordpress-magazine/6652608. In my local server, I can see the button, but on my online server, I can’t find it.

    Forum: Your WordPress
    In reply to: Help a newbie out!
    Thread Starter Bobozar

    (@skizzy)

    Hey bro tanx let me try it nd incase i dnt get it plz gimme ur contact so dat we cn chat thanks

Viewing 10 replies - 1 through 10 (of 10 total)