Wordpress Query Compare Meta Key to Page Title - Hack The Tech - Latest News related to Computer and Technology

Hack The Tech - Latest News related to Computer and Technology

Get Daily Latest News related to Computer and Technology and hack the world.

Thursday, September 7, 2023

Wordpress Query Compare Meta Key to Page Title

I created a shortcode that displays information on pages using a query of a custom post type I created. I want the query to display posts by comparing a meta field in the CPT to the title of the page that the shortcode is embedded on (i.e., the current page). Here's what I am working with so far:

   $post_name = get_the_title($post);
    
    $args = array(
       
        'post_type' => 'players',
        'author' =>  get_current_user_id(),
        'post_status' => array('registered', 'placed', 'paid'),
        'nopaging'=> true,
        'meta_query' => array(
            array(
                'key' => 'league',
                'value' => $post_name,
                'compare' => 'LIKE'
            )
        )
    );

This retrieves the posts matching the criteria of post type, user ID, and post status. However, my meta_query does not affect the filtering of post information. I might just be pulling the page_title incorrectly. From all of my stack and google browsing, there are a number of ways to do so.

For reference, the comparison in the meta_query would be comparing a meta value of "Fall Outdoor Soccer," then comparing that to the page title of "Fall Outdoor Soccer Schedule." From my understanding of LIKE, that should work.

Any suggestions on the correct way to pull this off?



source https://stackoverflow.com/questions/77054389/wordpress-query-compare-meta-key-to-page-title

No comments:

Post a Comment