Forum Replies Created

Viewing 15 replies - 1 through 15 (of 32 total)
  • Thread Starter pdg87

    (@pdg87)

    Okay I got it.. I had a pre_get_posts in my functions that was getting the main query and adding my custom post type to it when on categories, this was so that the custom post type showed in the categories, as they are not default posts. It was not set up accurately enought and was merging with this query:

    SELECT wp_posts.ID
    FROM wp_posts
    WHERE 1=1
    AND wp_posts.post_type = 'is_search_form'
    AND ((wp_posts.post_status = 'publish'))
    ORDER BY wp_posts.post_date DESC

    and as a result it was finding 50k results and running the below for the 50k results.

    SELECT option_value
    FROM wp_options
    WHERE option_name = 'is_search_69594'
    LIMIT 1

    thanks for your time and for the plugin. Have a great day.

    Thread Starter pdg87

    (@pdg87)

    No the shared code is WP core code, but this is what is being called 1000’s of times when I enable your plugin, I was wondering if you might have any idea why/what would cayse that. I will continue to debug. Thanks

    Thread Starter pdg87

    (@pdg87)

    Hi Vinod,

    The site is still local, I will see if I can get it on a test server. It’s the get_option function from wordpress option.php that seems to be causing this. If I comment this block ou within it the pages load okay:

    if ( false === $value ) {

    $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );

    // Has to be get_row() instead of get_var() because of funkiness with 0, false, null values.

    if ( is_object( $row ) ) {

    $value = $row->option_value;

    wp_cache_add( $option, $value, 'options' );

    } else { // Option does not exist, so we must cache its non-existence.

    $notoptions[ $option ] = true;

    wp_cache_set( 'notoptions', $notoptions, 'options' );

    /** This filter is documented in wp-includes/option.php */

    return apply_filters( "default_option_{$option}", $default_value, $option, $passed_default );

    }

    }

    again this only happens on category pages when the plugin is enabled. I guess it is doing an option lookup for every post for some reason..

    • This reply was modified 11 months, 4 weeks ago by pdg87.
    • This reply was modified 11 months, 4 weeks ago by pdg87.
    Thread Starter pdg87

    (@pdg87)

    Hi, thanks for the reply, I will get the info to you, but I think if you were to test it, it will work. It has worked eveyrtime I test it, I think its an edge case. I think the zones work okay, it is more the ajax failing to check the postcode I think, what triggers the check? any address field change? I’m wondering if I could force the check more often, specifically on keyup on postcode field for example

    You have an extra ‘<‘ tag after <body> tag.

    If you open you header.php file you should be able to see it and delete it.

    Thread Starter pdg87

    (@pdg87)

    Hi Chad,

    I finally got around to trying this out. This is a better example of what I am trying to acheive:

    http://wordpress.stackexchange.com/questions/191878/how-can-i-achieve-this-site-url-structure

    I couldntreally get the above working. So I am currently trying to use a custom post type with a custom taxonomy. Custom post type is ‘offers’ with a custom taxonomy ‘stores’. I am getting there but url’s are not behaving quite right.

    site.com/offers – loads
    site.com/offers/storename/offer-post – loads
    site.com/offers/storename – does not load (this should be a list of offers for that store taxonomy)

    Also, I am using breadcrumbs plugin and the link generated for this:

    displays as: site.com/offers/%stores_slug%/storename

    Something not quite right with my rewrite rules?

    Thread Starter pdg87

    (@pdg87)

    Ah that’s a shame seems close, maybe I should switch to using just custom fields?

    I’m going to carry on for now with some other areas of the website, but will address this again soon and see if I can use custom fields/make do with more simple query.

    I may drop you PM in the coming weeks if this blocks me too much. Would be great if you could help out in return for some beer money 🙂 Thanks again for the help!

    FWIW, _store_brands has always been in the db, I noticed it the other day when having a look with the value field_blah (I think this is he unique key for the custom field). What were you trying to do with this value? overwrite it with an array?

    Either way hopefully there’s some useful info in here for noobs like myself.

    Thread Starter pdg87

    (@pdg87)

    Thanks for sticking with it, but still the same output.. When you say update, Im just going to a store post which has the acf store_brands and clicking ‘update’ to save it, that’s what you mean? 🙂

    I’m starting to think I should just do away with the alternative_brand_names list and query store_brands as an array value against the single value of brand_name, which seems to work..

    Not ideal for my situation, but I could probably work around it as worse case.

    Thread Starter pdg87

    (@pdg87)

    No thats not a correct value for brand_name. Looking in the db, field_55590c17ac022 is a value for _store_brands.

    hmm looks like the data is not getting populated correctly..

    Array
    (
        [0] => field_55590c17ac022
    )
    SELECT wp_postmeta.meta_value FROM wp_posts
              INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )
              INNER JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id )
              WHERE wp_posts.post_type IN ( 'designers', 'stores' )
              AND wp_posts.post_status = 'publish'
              AND wp_posts.ID NOT IN ( 18 )
              AND (
              	   wp_postmeta.meta_key = 'alternative_brand_names'
    	           AND mt1.meta_key = 'brand_name'
    	           AND CAST( mt1.meta_value AS CHAR) IN (    'field_55590c17ac022' )
                  )
              GROUP BY wp_posts.ID
              ORDER BY wp_posts.post_date DESC
    Array
    (
    )
    Thread Starter pdg87

    (@pdg87)

    with the above, it just prints the post titles of all store posts e.g.

    Store1
    Store2

    I added a new store (store3) and left the store_brands field blank. This still shows up on the all store posts.

    Store1
    Store2
    store3

    are shown on every individual store post. (store1, store2, store3)

    printing $args gives this:

    Array ( [post_type] => Array ( [0] => designers [1] => stores ) [meta_query] => Array ( [relation] => OR [0] => Array ( [relation] => OR [0] => Array ( [key] => brand_name [value] => Array ( [0] => field_55590c17ac022 ) [compare] => IN ) [1] => Array ( [key] => _store_brands [value] => Array ( [0] => field_55590c17ac022 ) [compare] => IN ) ) ) )
    Thread Starter pdg87

    (@pdg87)

    Yeah sorry it’s confusing with similar CF and brand names and my php is terrible. I updated the functions.php file and then resaved a store post with store_brands CF. This is the code I have in single-stores.php (I changed the name of post type from brands to ‘designers’ and custom field name brands_name_alt to ‘alternative_brand_names’ as I had names them wrong in previous posts 🙂

    <?php 
    
    global $wpdb;
    
    $post_id    = absint( $post->ID );
    $post_types = array( 'designers', 'stores' );
    $list       = (array) get_post_meta( $post_id, "_store_brands", true );
    $array      = array_filter( array_map( 'trim', $list ) );
    
    if ( !empty( $array ) &&  $post_id ) {
    
    	$post_type_sql  = "'" . implode( "', '", array_map( 'esc_sql', $post_types ) ) . "'";
    	$meta_value_sql = "'" . implode( "', '", array_map( 'esc_sql', $array ) ) . "'";
    
    	// First query to get the brands_name_alt meta values
    	$query = "SELECT $wpdb->postmeta.meta_value FROM $wpdb->posts
              INNER JOIN $wpdb->postmeta ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id )
              INNER JOIN $wpdb->postmeta AS mt1 ON ( $wpdb->posts.ID = mt1.post_id )
              WHERE $wpdb->posts.post_type IN ( $post_type_sql )
              AND $wpdb->posts.post_status = 'publish'
              AND $wpdb->posts.ID NOT IN ( $post_id )
              AND (
              	   $wpdb->postmeta.meta_key = 'alternative_brand_names'
    	           AND mt1.meta_key = 'brand_name'
    	           AND CAST( mt1.meta_value AS CHAR) IN ( $meta_value_sql )
                  )
              GROUP BY wp_posts.ID
              ORDER BY wp_posts.post_date DESC";
    
    	$meta_values = $wpdb->get_col(  $query );
    
    	$args = array(
    		'post_type'    => $post_types,
    		'meta_query'   => array(
    
    			'relation' => 'OR',
    
    			array(
    
    				'relation' => 'OR',
    
    				array(
    					'key'     => 'brand_name',
    					'value'   => $array,
    					'compare' => 'IN'
    				),
    
    				array(
    					'key'     => '_store_brands',
    					'value'   => $array,
    					'compare' => 'IN'
    				),
    			)
    		),
    	);
    
    	// Check if meta values were found for the brands_name_alt key
    	if ( !empty( $meta_values ) ) {
    
    		// merge all meta values
    		$all_values = array();
    		foreach ( $meta_values as $value ) {
    			$value = array_filter( array_map( 'trim', (array)  explode( ',', $value ) ) );
    			$all_values = array_merge( $all_values, $value );
    		}
    
    		// Query for all posts with the store_brands values in the brands_name_alt values
    		$args['meta_query'][] =  array(
    			'key'     => '_store_brands',
    			'value'   => $all_values,
    			'compare' => 'IN'
    		);
    	}
    	$custom_query = new WP_Query($args);
    }
    
    while($custom_query->have_posts()) : $custom_query->the_post(); ?>
    
    	<div <?php post_class(); ?> id="post-<?php the_ID(); ?>">
    		<h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
    		<?php the_content(); ?>
    	</div>
    
    <?php endwhile; ?>
    <?php wp_reset_postdata(); // reset the query ?>

    There is a good chance there is something wrong with what I have added. The above just returns both store posts I have made.

    If I move the while statement above the last trailing { the page doesn’t load.

    Thread Starter pdg87

    (@pdg87)

    Wow thanks for that, I was about to give up on it. However I think this will return stores? rather than brands?

    the list array (value?) should be made from the csv store_brands from the stores post type.

    Then this is checked against brand_name (key?) and brands_name_alt(key?) custom field in the brand post type.

    To return a list of brand posts on a store post.

    Hopefully just a case of changing some of the field names around?

    pdg87

    (@pdg87)

    Hey, no problem, Its just some margin gettin applied still to the header element.

    If you change:

    <header class="entry-header">
    <header class="entry-header-single">

    It should work.

    Basically just changing the element class names adding ‘-single’ this way it doesn not pick up any styling from the other page elements and we could style these elements differently if needed.

    This wont cause any layout problems. Just if you ever want an post to include the large header image. You would have to set up a custom template for certain posts to acheive this.

    These changes will show on every post. So the layout will be like this.

    Thread Starter pdg87

    (@pdg87)

    Ah, thought so Keesie 🙁 There could be 100+ brands to enter into store_brands for every store so it would take ages to have seperate fields for each brand..

    Any other options? Is it achievable using wpdb sql query?

    Or someway to break csv entered values into seperate fields?

    Thread Starter pdg87

    (@pdg87)

    great, I will report back. Thanks for taking your time to help Keesie, Much apprecited!

Viewing 15 replies - 1 through 15 (of 32 total)