PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, July 8, 2022

[FIXED] how to display popular posts by multiple meta keys in wordpress?

 July 08, 2022     php, posts, wordpress     No comments   

Issue

I want to display popular post in custom page , I have added post views and post like option in wordpress and I want to show popular posts based views and likes,

I tried this code but it's ranking only for views counter ;

$args = array(
  'posts_per_page'=>5,
    'orderby' => 'meta_value',
  'order' => 'DESC',
  'meta_query'      => array(
    'relation'    => 'AND',
    '_post_views' => array(
      'key'     => '_post_views',
      'type'    => 'NUMERIC',
      'compare' => 'LIKE'
    ),
    '_post_like_count'    => array(
      'key'     => '_post_like_count',
      'type'    => 'NUMERIC',
      'compare' => 'LIKE'
    ),
),
);

thanks for answers


Solution

Try new orderby feature:

$args = array(
    'posts_per_page'  => 5,
    'meta_query'      => array(
        'relation'    => 'AND',
        '_post_views' => array(
            'key'     => '_post_views',
            'type'    => 'NUMERIC',
            'compare' => 'LIKE'
        ),
        '_post_like_count' => array(
            'key'     => '_post_like_count',
            'type'    => 'NUMERIC',
            'compare' => 'LIKE'
        ),
    ),
    'orderby' => array(
        '_post_views' => 'DESC',
        '_post_like_count' => 'DESC'
    )
);


Answered By - dan9vu
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing