• Hi there,

    I am running this plugin on a WP install with a custom post type that has as a custom post field called ‘popularity’ (formatted as “number”).

    Now, I want to create a list, that only shows posts with a popularity value below the value of 10.

    To do so, I have followed Tom’s instructions here and adjusted the plugin itself to allow for additional arguments.

    I now use the following code, as suggested in this support article, but unfortunately, I receive the following fatal error:

    Uncaught Error: syntax error, unexpected '=>' (T_DOUBLE_ARROW)

    This is the code I use in my functions.php:

    add_filter( 'wp_show_posts_shortcode_args', function( $args, $settings ) {
        if ( 4566 === $settings['list_id'] ) {
            $args['meta_query'] => array(
                array(
                    'key' => 'popularity',
                    'value' => 10,
                    'compare' => '<',
                )
            );
        }
    
        return $args;
    } );

    What do I do wrong?

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Support Elvin

    (@ejcabquina)

    Hi there,

    On this line – $args['meta_query'] => array( – change the => to =.

    And test the code again. Let us know how it goes. ??

    Thread Starter ermilis

    (@ermilis)

    Thanks, for your reply.

    I just tried your suggestion and changed the code to:

    add_filter( 'wp_show_posts_shortcode_args', function( $args, $settings ) {
        if ( 4566 === $settings['list_id'] ) {
            $args['meta_query'] = array(
                array(
                    'key' => 'popularity',
                    'value' => 10,
                    'compare' => '<',
                )
            );
        }
    
        return $args;
    } );

    However, the result is:

    Your PHP code changes were rolled back due to an error on line 30 of file wp-content/themes/generatepress_child/functions.php. Please fix and try saving again.
    
    Uncaught ArgumentCountError: Too few arguments to function {closure}(), 1 passed in wp-includes/class-wp-hook.php on line 305 and exactly 2 expected in wp-content/themes/generatepress_child/functions.php:30
    Stack trace:
    #0 wp-includes/class-wp-hook.php(305): {closure}(Array)
    #1 wp-includes/plugin.php(189): WP_Hook->apply_filters(Array, Array)
    #2 wp-content/plugins/wp-show-posts/wp-show-posts.php(383): apply_filters('wp_show_posts_s...', Array, Array)
    #3 wp-content/plugins/wp-show-posts/wp-show-posts.php(570): wpsp_display('3864', '')
    #4 wp-includes/shortcodes.php(356): wpsp_shortcode_function(Array, '', 'wp_show_posts')
    #5 [internal function]: do_shortcode_tag(Array)
    #6 wp-includes/shortcodes.php(228): preg_replace_callback('/\\[(\\[?)(wp_sho...', 'do_shortcode_ta..

    Any idea of what to do?

    Thanks for your help!

    Plugin Support Elvin

    (@ejcabquina)

    On the last 2 lines, can you change this,

    return $args;
    } );

    to this.

    return $args;
    }, 15, 2 );

    Normally we can just omit this but since we’re not using function names, let’s add the priority and # of arguments.

    Thread Starter ermilis

    (@ermilis)

    Thanks! It now does not throw any error any longer. But the actual list shows no values – instead it throws the “no results message”. ??

    However, I know and have double-checked that there are a few custom posts matching the criteria (of having a popularity value < 10).

    Any other idea? Sorry for bugging you..

    I tried to find the exact query via query monitor that uses WP_Query->get_posts and found the following:

    SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
    FROM wp_posts
    INNER JOIN wp_postmeta
    ON ( wp_posts.ID = wp_postmeta.post_id )
    WHERE 1=1
    AND ( ( wp_postmeta.meta_key = 'popularity'
    AND wp_postmeta.meta_value < '10' ) )
    AND wp_posts.post_type = 'trees'
    AND ((wp_posts.post_status = 'publish'))
    GROUP BY wp_posts.ID
    ORDER BY 1
    LIMIT 0, 50 /* From [www.trees.com/beliebte-hunderassen/] in [/nas/content/live/trees/wp-content/plugins/wp-show-posts/wp-show-posts.php:383] */
    Plugin Support Elvin

    (@ejcabquina)

    That query monitor only tells what was queried.

    Can you verify if there are any existing post on the custom post type trees with custom field popularity with value that’s less than 10?

    Thread Starter ermilis

    (@ermilis)

    Thanks for your reply!

    In regards to your question:
    correct, I can verify that there are existing posts in the custom post type trees with the custom field popularity with a numeric value below 10.

    Any other idea or change I could try?

    I tried to understand this article by ACF on how to query posts by custom fields (within a custom post type), but couldn’t get it to work, unfortunately.

    Thanks for your effort.. really appreciate it

    Plugin Support Elvin

    (@ejcabquina)

    Strange.

    Similar code works for everyone else. (assuming the required modification on the plugin changes or if you’re using the latest beta version found here – https://wpshowposts.com/wp-show-posts-1-2-0/ AND the ID is correct)

    Can you try this?

    add_filter( "wp_show_posts_shortcode_args", function ($args, $settings) {
        if (4566 === (int) $settings["list_id"]) {
            $args['post_type'] = 'trees';
            $args['meta_query'] = array(
    			'relation' => 'AND',
                array( 
    				'key' => 'popularity',
    				'compare' => 'EXISTS'
    			),
    			array( 
                    'key' => 'popularity',
                    'value' => 10,
                    'compare' => '<',
    			),
    		);
        }
        return $args;
    },10,2);
Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Filter by custom post field’ is closed to new replies.