0

I code

$args = array(
     'post_type' => array('product', 'product_variation'),     
     'posts_per_page' => 2000, 
     'category__and' => 'No description tecnobath'

);
$loop = new WP_Query( $args );
$contador = 0;
$productos = [];
$producto = (object) array(
    'name' => '',
    'sku' => '',
    'categories' => '',
    'price' => 0
);

while ( $loop->have_posts() ) : $loop->the_post(); 
    global $product;
    $producto['name'] = the_title();
    $producto['sku'] = $product->get_sku();
    $producto['price'] = $product->get_price();
    $producto['categories'] = $product->get_categories();
    array_push($productos, $producto);

endwhile;
wp_reset_query(); 

var_dump($productos); 

But it's not working.

In the page where I put the shorcode get:

Fatal error: Uncaught Error: Call to undefined function is_user_logged_in() in C:\laragon\www\cotizador\wp-includes\class-wp-query.php:2352 Stack trace: #0 C:\laragon\www\cotizador\wp-includes\class-wp-query.php(3230): WP_Query->get_posts() #1 C:\laragon\www\cotizador\wp-includes\class-wp-query.php(3336): WP_Query->query(Array) #2 C:\laragon\www\cotizador\wp-content\plugins\cotizador\inc\products.php(9): WP_Query->__construct(Array) #3 C:\laragon\www\cotizador\wp-content\plugins\cotizador\cotizador.php(32): include('C:\laragon\www\...') #4 C:\laragon\www\cotizador\wp-settings.php(305): include_once('C:\laragon\www\...') #5 C:\laragon\www\cotizador\wp-config.php(76): require_once('C:\laragon\www\...') #6 C:\laragon\www\cotizador\wp-load.php(37): require_once('C:\laragon\www\...') #7 C:\laragon\www\cotizador\wp-blog-header.php(13): require_once('C:\laragon\www\...') #8 C:\laragon\www\cotizador\index.php(17): require('C:\laragon\www\...') #9 {main} thrown in C:\laragon\www\cotizador\wp-includes\class-wp-query.php on line 2352

And in the archive location:

Fatal error: Uncaught Error: Class 'WP_Query' not found in C:\laragon\www\cotizador\wp-content\plugins\cotizador\inc\products.php:9 Stack trace: #0 {main} thrown in C:\laragon\www\cotizador\wp-content\plugins\cotizador\inc\products.php on line 9 Blockquote

  1. Plugins
    • cotizador
      • inc
        • products.php (here is the code)
      • views
        • cotizadorV.php
    • cotizador.php

cotizador.php

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly.
}

/* Añade los estilos */
include('inc/products.php');

function ctz_show() {

    include('views/cotizadorV.php'); }

add_shortcode( 'cotizador', 'ctz_show' );

cotizadorV.php

<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly.
}

echo "Hola from CV";
  • 1
    Where is this code running? It sounds like you may have it set up independently so WP isn't being loaded. – WebElaine Jul 6 '18 at 17:24
  • I add the project structure – Cragser Jul 6 '18 at 17:41
  • 1
    You say In the page where I put the shortcode- I don't see a shortcode defined anywhere, you just have code that will run as soon as the plugin is loaded, which will break every front and back-end page on your site. What are you trying to do exactly? – Milo Jul 6 '18 at 17:50
  • Sorry, Sorry, I did not copy what I had done from the shortcode, I updated it – Cragser Jul 6 '18 at 17:54
  • 1
    And this: include('inc/products.php'); THat code is just running in the middle of the plugin files. Are you expecting the results of that code to appear where the shortcode is? In which case include('inc/products.php'); should be inside function ctz-show and ctz_show should return the output. – anmari Jul 7 '18 at 7:07

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.