0

In order to query products based on custom fields, from the plugin advanced custom fields, i have to use this wp query script, to query it correctly.

However, when i copied this script, changed the needed values, and inserted it into functions.php, it crashes the site.

I believe its a parse or syntax error because its using a lot of . I have tried removing some of them, still no results. When all the other scripts above it has been removed (irrelevant), then it looks like this:

    <?php
<?php
add_action( 'wp_ajax_posts_by_brand_action', 'posts_by_brand_action_callback' );

function posts_by_brand_action_callback() {
    global $wpdb; // this is how you get access to the database

    $brand = $_POST['brand'];

        echo $whatever;

  // ----- START OF BRAND FILTER CODE


  // args
  $args = array(
    //'numberposts' => -1,
    //'post_type'       => 'event',
    'meta_key'      => 'brand',
    'meta_value'    => $brand
  );


  // query
  $the_query = new WP_Query( $args );

  <?php if( $the_query->have_posts() ): ?>
    <ul>
    <?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
      <li>
        <a href="<?php the_permalink(); ?>">
          <?php the_title(); ?>
        </a>
      </li>
    <?php endwhile; ?>
    </ul>
  <?php endif; ?>

  <?php wp_reset_query();    // Restore global post data stomped by the_post(). ?>

  // ------ END OF BRAND FILTER CODE

    wp_die(); // this is required to terminate immediately and return a proper response
}
?>

How do i go a about this? Would it be correct to say, that shouldn't be used inside an ?

  • Remove ?> in the line above // ------ END OF BRAND FILTER CODE – Nabil Kadimi Oct 22 '16 at 5:16
2

It's hard to debug only seeing a portion of your code.

For starters, you've got two <?php opening tags at the top which would certainly cause an error. But I think that's leftover from your copy and paste.

  • Look at your php server log which will tell you what file and line number the error is occurring.
  • Put the following in your wp-config.php file which will print all errors in your browser. Then you won't have to access the php log directly.

Code to add:

define( 'WP_DEBUG', true );

WordPress Debugging Guide

0

Why are you opening so many php tags without any reason? Here is the reviewed code, please check if it works:

<?php
add_action( 'wp_ajax_posts_by_brand_action', 'posts_by_brand_action_callback' );

function posts_by_brand_action_callback() {
    global $wpdb; // this is how you get access to the database

    $brand = $_POST['brand'];

    echo $whatever;

  // ----- START OF BRAND FILTER CODE


  // args
  $args = array(
    //'numberposts' => -1,
    //'post_type'       => 'event',
    'meta_key'      => 'brand',
    'meta_value'    => $brand
  );


  // query
  $the_query = new WP_Query( $args );

  if($the_query->have_posts() ):
?>
    <ul>
    <?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
      <li>
        <a href="<?php the_permalink(); ?>">
          <?php the_title(); ?>
        </a>
      </li>
    <?php endwhile; ?>
    </ul>
  <?php
  endif;

  wp_reset_query();  // Restore global post data stomped by the_post().

  // ------ END OF BRAND FILTER CODE

    wp_die(); // this is required to terminate immediately and return a proper response
}
?>
  • The way i inserted it, it didn't work. I have created a gist, which stores the whole functions file, with all the other non related code. gist.github.com/anonymous/4d1f57c5875eaf54ed4f23afe5e257b3 – Magnus Pilegaard Oct 22 '16 at 11:04
  • Set debug mode true to get the actual problem and please try after removing this theme_enqueue_styles function from functions.php – amit singh Oct 24 '16 at 8:23
  • I was told to have it, so it knew what style to access. It's a child theme. – Magnus Pilegaard Oct 27 '16 at 22:30

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.