Get all posts by post meta key and meta value

Description

For some situations, we need to get all the posts which have specific meta key and meta value.

We can do it with the help of the WP_Query class.

Example

Note: I’m giving you an imaginary example just for a reference to understand, In which situation you can use below code snippet.

Suppose,

  • We have a custom post type properties.
  • We have 15 properties are published.
  • We have store the location of each property in meta key property-location
  • And, We want to get all the properties which meta key is property-location with meta value Pune.

Code Snippet

$query_args = array(
	'post_type'  => 'properties',
	'meta_query' => array(
	    array(
			'key'   => 'property-location',
			'value' => 'Pune',
	    ),
	)
);

$query = new WP_Query( $query_args );

Here,
post_type is our custom post type slug. In our example its properties.
key is meta key. In our example its property-location
value is meta value. In our example its Pune

Gist Snippet

You can use below complete gist code snippet for reference.
Note: In below code snippet you need to change the parameters as you need.


Here,
I have added some extra parameters to optimize the WordPress query.

  • fields with value ids which return ONLY array post IDs of all found items.
  • no_found_rows with value true which optimizes the query.
  • posts_per_page with value -1 to get all the posts. Default it return only 10 items.

Show all READY scheduled events OR READY corn jobs in WordPress

Description

By using the function wp_get_ready_cron_jobs() to get all the READY scheduled events OR READY corn jobs.

Note: Use below code snippet for ONLY debugging/development purpose.

Code Snippet

Output

Array
(
    [1551289036] => Array
        (
            [wp_import_astra_sites_cron] => Array
                (
                    [40cd750bba9870f18aada2478b24840a] => Array
                        (
                            [schedule] => wp_import_astra_sites_cron_interval
                            [args] => Array
                                (
                                )

                            [interval] => 300
                        )

                )

        )

)

Show all scheduled events OR corn jobs in WordPress

Description

By using the funciton wp_get_schedules () to get all the scheduled events OR corn jobs.

Note: Use below code snippet for ONLY debugging/development purpose.

Code Snippet

Output

Array
(
    [wp_import_astra_single_site_cron_interval] => Array
        (
            [interval] => 300
            [display] => Every 5 Minutes
        )

    [wp_import_astra_images_site_cron_interval] => Array
        (
            [interval] => 300
            [display] => Every 5 Minutes
        )

    [wp_import_astra_site_terms_cron_interval] => Array
        (
            [interval] => 300
            [display] => Every 5 Minutes
        )

    [wp_import_astra_sites_cron_interval] => Array
        (
            [interval] => 300
            [display] => Every 5 Minutes
        )

    [hourly] => Array
        (
            [interval] => 3600
            [display] => Once Hourly
        )

    [twicedaily] => Array
        (
            [interval] => 43200
            [display] => Twice Daily
        )

    [daily] => Array
        (
            [interval] => 86400
            [display] => Once Daily
        )

)

Check which template loading in WordPress site

Below is the simple function which you can use for debugging the site. It  display the template file path of the current page. Like below image:

Add below code snippet into your themes functions.php file.